-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEggDropProblem.cpp
More file actions
62 lines (52 loc) · 1.53 KB
/
EggDropProblem.cpp
File metadata and controls
62 lines (52 loc) · 1.53 KB
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
#include <iostream>
#include <vector>
#include <algorithm>
class EggDropSimulator
{
private:
int eggs;
int floors;
public:
EggDropSimulator(int numEggs, int numFloors)
: eggs(numEggs), floors(numFloors)
{
}
int findPivotalFloor()
{
std::vector<std::vector<int>> dp(eggs + 1, std::vector<int>(floors + 1, 0));
// Base condition 1: If number of eggs is 0, we can't do anything
// Base condition 2: If number of floors is 0, we can't do anything
// Base condition 3: If number of eggs is 1, worst case depends on number of floors
for (size_t i = 1; i < dp[0].size(); ++i)
dp[1][i] = i;
// Base condition 4: If number of floors is 1, worst case depends drops is always 1
for (size_t i = 1; i < dp.size(); ++i)
dp[i][1] = 1;
for (size_t i = 2; i < dp.size(); ++i)
{
for (size_t j = 2; j < dp[0].size(); ++j)
{
// For a given question dp[i][j], we need to look
// at the worst case from dp[i][1] to dp[i][j]
dp[i][j] = std::numeric_limits<int>::max();
for (size_t k = 1; k <= j; ++k)
{
int max_value = std::max(dp[i][j - k], dp[i - 1][k - 1]) + 1;
// We want minimum number of turns from the worst case
if (max_value < dp[i][j])
dp[i][j] = max_value;
}
}
}
return dp[eggs][floors];
}
};
int main()
{
int numEggs = 2;
int numFloors = 100;
EggDropSimulator sim(numEggs, numFloors);
int numSims = sim.findPivotalFloor();
std::cout << "Worst case number of turns for " << numEggs << " eggs and " << numFloors << " floors is " << numSims << std::endl;
return 0;
}