-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathegg_dropping_puzzle.cpp
More file actions
48 lines (33 loc) · 837 Bytes
/
egg_dropping_puzzle.cpp
File metadata and controls
48 lines (33 loc) · 837 Bytes
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
#include <iostream>
#define MAX_EGGS 100
#define MAX_FLOORS 100
#define INF 1000000000
using namespace std;
int memo[MAX_EGGS][MAX_FLOORS];
/*
* Returns the minimum number of attempts
* needed in the worst case for n eggs
* and k floors.
* Time complexity: O(n*k^2)
*/
int eggDrop(int n, int k) {
if(k == 0) return 0;
if(k == 1) return 1;
if(n == 1) return k;
if(memo[n][k] > -1) return memo[n][k];
int ans = INF;
for(int h = 1; h <= k; ++h) {
ans = min(ans, max(
eggDrop(n-1, h-1),
eggDrop(n, k-h)
));
}
return memo[n][k] = ans + 1;
}
int main() {
memset(memo, -1, sizeof memo);
cout << eggDrop(2, 100) << '\n';
cout << eggDrop(10, 5) << '\n';
cout << eggDrop(10, 100) << '\n';
return 0;
}