-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1884_EggDropWith2EggsAndNFloors.cpp
46 lines (37 loc) · 1.1 KB
/
LC_1884_EggDropWith2EggsAndNFloors.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
/*
1884. Egg Drop With 2 Eggs and N Floors
https://leetcode.com/problems/egg-drop-with-2-eggs-and-n-floors/
*/
//https://brilliant.org/wiki/egg-dropping/
class Solution {
public:
int twoEggDrop(int k) {
// memo.resize(2+1, vector<int>(k+1, -1));
// return solve(2, k);
return ceil(-0.5+ (sqrt(1+8*k)/2));
}
vector<vector<int>> memo;
int solve(int e, int f)
{
if(f==0 || f==1)
return memo[e][f]=f;
if(e == 1)
return memo[e][f]=f;
if(memo[e][f] != -1)
return memo[e][f];
int mn = INT_MAX;
for(int x=1; x<=f; x++)
{
// int ifbreaks = solve(e-1, x-1);
// int ifnotbreaks = solve(e, f-x);
// int ops = 1 + max(ifbreaks, ifnotbreaks);
int ops = 1 + max(solve(e-1, x-1), solve(e, f-x));
// cout<<e<<" "<<x<<" "<<ops<<") ";
if(ops < mn)
mn = ops;
}
// cout<<e<<" "<<f<<" "<<mn<<") ";
// cout<<endl;
return memo[e][f]=mn;
}
};