Skip to content

Commit e1b27e2

Browse files
authored
Create EggDroppingPuzzle.java
1 parent 9355b9e commit e1b27e2

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
https://www.geeksforgeeks.org/egg-dropping-puzzle-dp-11/
3+
4+
Question: You are given n eggs and specified a number of k floors. Write an algorithm to find the minimum number of drops is required to know the floor from which if the egg is dropped, it will break.
5+
Assumptions -
6+
…..An egg that survives a fall can be used again.
7+
…..A broken egg must be discarded.
8+
…..The effect of a fall is the same for all eggs.
9+
…..If an egg breaks when dropped, then it would break if dropped from a higher floor.
10+
…..If an egg survives a fall then it would survive a shorter fall.
11+
12+
Optimal Substructure
13+
When we drop an egg from a floor x, there can be two cases (1) The egg breaks (2) The egg doesn’t break.
14+
15+
1) If the egg breaks after dropping from xth floor, then we only need to check for floors lower than x with remaining eggs; so the problem reduces to x-1 floors and n-1 eggs
16+
2) If the egg doesn’t break after dropping from the xth floor, then we only need to check for floors higher than x; so the problem reduces to k-x floors and n eggs.
17+
18+
Since we need to minimize the number of trials in worst case, we take the maximum of two cases. We consider the max of above two cases for every floor and choose the floor which yields minimum number of trials.
19+
20+
k ==> Number of floors
21+
n ==> Number of Eggs
22+
eggDrop(n, k) ==> Minimum number of trials needed to find the critical
23+
floor in worst case.
24+
eggDrop(n, k) = 1 + min{max(eggDrop(n - 1, x - 1), eggDrop(n, k - x)):
25+
x in {1, 2, ..., k}}
26+
27+
*/
28+
29+
//DYNAMIC PROGRAMMING - BOTTOM UP APPROACH
30+
31+
public int minEggDrops(int eggs, int floors){
32+
33+
if(floors==0 || floors==1 || eggs==1) return floors;
34+
35+
int drops[][] = new int[eggs+1][floors+1];
36+
37+
//when we have only one floor --> #eggs does not matter --> only one drop rqd
38+
for(int i=1 ; i <= eggs ; i++) drops[i][1] = 1;
39+
40+
//when we have only one egg --> we need to drop it from floor1 then floor2 then floor3...--> In worst case #drops = #floors
41+
for(int j=1 ; j <= floors ; j++) drops[1][j] = j;
42+
43+
for(int i=2 ; i <= eggs ; i++){
44+
45+
for(int j=2 ; j <= floors ; j++){
46+
47+
drops[i][j] = Integer.MAX_VALUE;
48+
49+
//simulating the drop from each floor in the range [1...j]
50+
for(int k=1 ; k <= j ; k++){
51+
52+
/*
53+
considering the WORST CASE by choosing the maximum number of drops from each floor
54+
drops[i-1][k-1] --> egg breaks, #floors under consideration reduces by 1 --> all floors below the current floor
55+
drops[i][j-k] --> egg does not break, #floors under consideration --> all floors above the current floor
56+
*/
57+
58+
int maxDropsForWorstCase = 1 + Math.max(drops[i-1][k-1], drops[i][j-k]);
59+
60+
/*
61+
After looking through all the floors ie. the maximum number of drops required from each floor,
62+
we chose the floor which gives us the minimum number of drops in the worst case.
63+
ie. We look through the maximum number of drops required for each floor and chose the floor which
64+
results in minimum of all the worst cases
65+
*/
66+
67+
if(drops[i][j] > maxDropsForWorstCase){
68+
drops[i][j] = maxDropsForWorstCase;
69+
}
70+
}
71+
}
72+
}
73+
74+
return drops[eggs][floors];
75+
}
76+
77+
/*
78+
Time Complexity - O(eggs * floors^2)
79+
Space Complexity - O(eggs * floors)
80+
*/

0 commit comments

Comments
 (0)