-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEggDropping.java
47 lines (42 loc) · 1.24 KB
/
EggDropping.java
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
public class EggDropping {
public int calculate(int eggs, int floors){
int T[][] = new int[eggs+1][floors+1];
int c =0;
for(int i=0; i <= floors; i++){
T[1][i] = i;
}
for(int e = 2; e <= eggs; e++){
for(int f = 1; f <=floors; f++){
T[e][f] = Integer.MAX_VALUE;
for(int k = 1; k <=f ; k++){
c = 1 + Math.max(T[e-1][k-1], T[e][f-k]);
if(c < T[e][f]){
T[e][f] = c;
}
}
}
}
return T[eggs][floors];
}
public int calculateRecursive(int eggs, int floors){
if(eggs == 1){
return floors;
}
if(floors == 0){
return 0;
}
int min = 1000;
for(int i=1; i <= floors; i++){
int val = 1 + Math.max(calculateRecursive(eggs-1, i-1),calculateRecursive(eggs, floors-i));
if(val < min){
min = val;
}
}
return min;
}
public static void main(String args[]){
EggDropping ed = new EggDropping();
int r = ed.calculate(3,100);
System.out.println(r);
}
}