-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEggDroppingPuzzle.cpp
69 lines (52 loc) · 1.31 KB
/
EggDroppingPuzzle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include<iostream>
#include<climits>
using namespace std;
int eggDroppingDP(int number_of_eggs,int total_floors){
int dp[100][100];
for(int i=0;i<=number_of_eggs;i++){
dp[i][0] = 0;
dp[i][1] = 1;
}
for(int i=0;i<=total_floors;i++){
dp[0][i] = 0;
dp[1][i] = i;
}
for(int i=2;i<=number_of_eggs;i++){
for(int j=2;j<=total_floors;j++){
int minimum = INT_MAX;
int current;
for(int x=1;x<=j;x++){
current = max(dp[i-1][x-1],dp[i][j-x])+1;
if(current<minimum){
minimum = current;
}
}
dp[i][j] = minimum;
}
}
int ans = dp[number_of_eggs][total_floors];
return ans;
}
//Returns the min no of droppings required in the worst case.
int eggDroppingRecursive(int number_of_eggs,int total_floors){
if(number_of_eggs==1){
return total_floors;
}
if(total_floors==1){
return 1;
}
if(total_floors==0){
return 0;
}
int minimum = INT_MAX;
for(int kth_floor = 1;kth_floor<=total_floors;kth_floor++){
int ans = max( eggDroppingRecursive(number_of_eggs-1,kth_floor-1),eggDroppingRecursive(number_of_eggs,total_floors-kth_floor));
minimum = min(minimum,ans);
}
return minimum+1;
}
int main(){
cout<<eggDroppingRecursive(10,10)<<endl;
cout<<eggDroppingDP(2,36)<<endl;
return 0;
}