-
Notifications
You must be signed in to change notification settings - Fork 484
/
EggDroppingPuzzle.cpp
55 lines (52 loc) Β· 1.03 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
#include<bits/stdc++.h>
using namespace std;
class Solution
{
int dp[201][201] ;
public:
int solve(int e , int f)
{
if(f==0 || f==1)
{
return dp[e][f]=f ;
}
if(e==1)
{
return dp[e][f]=f ;
}
if(dp[e][f]!=-1)
{
return dp[e][f] ;
}
int finalAns = INT_MAX ;
for(int k=1; k<=f ; k++)
{
int tempAns= 1+ max(solve(e-1, k-1), solve(e, f-k)) ;
finalAns = min(finalAns , tempAns) ;
}
return dp[e][f] = finalAns ;
}
int eggDrop(int n, int k)
{
memset(dp, -1, sizeof(dp)) ;
solve(n , k) ;
}
};
// { Driver Code Starts.
int main()
{
//taking total testcases
int t;
cin>>t;
while(t--)
{
//taking eggs and floors count
int n, k;
cin>>n>>k;
Solution ob;
//calling function eggDrop()
cout<<ob.eggDrop(n, k)<<endl;
}
return 0;
}
// } Driver Code Ends