forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEgg_Drop.c
54 lines (50 loc) · 1.4 KB
/
Egg_Drop.c
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
# include <stdio.h>
// 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 = 9999;
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 = 2, floors = 10;
printf("The number of eggs: %d\n", eggs);
printf("The number of floors: %d\n", floors);
printf("Minimum number of trials: %d ", eggDrop(eggs, floors));
return 0;
}
/*The number of eggs: 2
The number of floors: 10
Minimum number of trials: 4*/