Skip to content

Commit b375ed6

Browse files
committed
eggs
1 parent 64760a8 commit b375ed6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

src/DP/EggFloor.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package DP;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* Created by dheeraj on 8/7/16.
7+
*/
8+
public class EggFloor {
9+
10+
private static int[][] sol;
11+
12+
private static int trails(int eggs, int floors) {
13+
if (sol[eggs][floors] != Integer.MAX_VALUE) {
14+
return sol[eggs][floors];
15+
}
16+
int ans = Integer.MAX_VALUE;
17+
if (eggs == 1) {
18+
//drop from each floor
19+
ans = floors;
20+
}else if (floors == 0 || floors == 1) {
21+
ans = floors;
22+
}else {
23+
for (int x = 1; x < floors; x++) {
24+
int broken = trails(eggs - 1, x - 1);
25+
int unbroken = trails(eggs, floors - x);
26+
ans = Math.min(ans, Math.max(broken, unbroken));
27+
}
28+
ans = ans+1;
29+
}
30+
sol[eggs][floors] = ans;
31+
return ans;
32+
}
33+
34+
public static void main(String[] strings) {
35+
int floors = 10;
36+
int eggs = 2;
37+
sol = new int[eggs+1][floors + 1];
38+
for(int x =0;x<=eggs;x++)
39+
Arrays.fill(sol[x], Integer.MAX_VALUE);
40+
41+
trails(eggs, floors);
42+
System.out.println(sol[eggs][floors]);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)