Skip to content

Commit 94f8b9e

Browse files
committed
🔨 Egg Dropping Puzzle
1 parent a763eba commit 94f8b9e

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class EggDroppingPuzzle {
2+
3+
public static int EggDrops(int noOfEggs, int noOfFloors){
4+
int [][] resultantMatrix = new int[noOfEggs+1][noOfFloors+1];
5+
6+
for(int i=1; i<=noOfEggs; i++){
7+
resultantMatrix[i][0] = 0;
8+
resultantMatrix[i][1] = 1;
9+
}
10+
11+
for(int j=1; j<=noOfFloors; j++)
12+
resultantMatrix[1][j] = j;
13+
14+
for(int i=2; i<=noOfEggs; i++){
15+
for(int j=2; j<=noOfFloors; j++){
16+
resultantMatrix[i][j] = Integer.MAX_VALUE;
17+
for(int k=1; k<=j ; k++)
18+
{
19+
int value = 1 + Math.max(resultantMatrix[i-1][k-1], resultantMatrix[i][j-k]);
20+
if(value < resultantMatrix[i][j])
21+
resultantMatrix[i][j] = value;
22+
}
23+
}
24+
}
25+
return resultantMatrix[noOfEggs][noOfFloors] ;
26+
}
27+
28+
public static void main(String[] args) {
29+
int noOfEggs = 2;
30+
int noOfFloors = 18;
31+
32+
System.out.println("Minimum number of trials in worst case is: "+EggDrops(noOfEggs, noOfFloors));
33+
}
34+
}

0 commit comments

Comments
 (0)