File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments