forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEgg_Drop.cpp
59 lines (54 loc) · 1.47 KB
/
Egg_Drop.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
# include <stdio.h>
# include <iostream>
using namespace std;
// To get maximum of two integers
#define max(a, b) ((a > b)? a : b)
int eggDrop(int eggs, int floors)
{
/* A 2D table where entery eggFloor[i][j] will represent minimum
number of trials needed for e eggs and f floors. */
int eggfloor[eggs][floors];
for (int e = 1; e <= eggs; e++)
{
for (int f = 0; f <= floors; f++)
{
if(f == 0)
{
eggfloor[e][f] = 0;
}
else if(e == 1)
{
eggfloor[e][f] = f;
}
else
{
// Fill rest of the entries in table using optimal substructure
// property
int min = INT_MAX;
for (int i = 1; i <= f; i++)
{
int sol = max(eggfloor[e-1][i-1], eggfloor[e][f-i]) + 1;
if(sol < min)
{
min = sol;
}
}
eggfloor[e][f] = min;
}
}
}
return eggfloor[eggs][floors];
}
int main()
{
int eggs ,floors;
cout << "Enter the number of eggs: ";
cin >> eggs;
cout << "Enter the number of floors: ";
cin >> floors;
cout << "Minimum number of trials: " << eggDrop(eggs, floors) << endl;
return 0;
}
/*Enter the number of eggs: 2
Enter the number of floors: 10
Minimum number of trials: 4*/