-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEgg Dropping Problem.java
More file actions
58 lines (38 loc) · 1.34 KB
/
Egg Dropping Problem.java
File metadata and controls
58 lines (38 loc) · 1.34 KB
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
48
49
50
51
52
53
54
55
56
57
58
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
class EggDrop{
//https://practice.geeksforgeeks.org/problems/egg-dropping-puzzle/0
public static Scanner scn = new Scanner(System.in);
public static void main (String[] args) throws java.lang.Exception{
EggDrop ED = new EggDrop();
int T = scn.nextInt();
while(T-- > 0){
int eggs = scn.nextInt();
int floors = scn.nextInt();
HashMap<Integer, HashMap<Integer, Integer>> map = new HashMap<>();
int ans = ED.dropEggs(eggs, floors, map);
System.out.println(ans);
}
}
public static int dropEggs(int eggs, int floors, HashMap<Integer, HashMap<Integer, Integer>> map){
if(eggs == 1)
return floors;
if(floors == 0)
return 0;
if(map.containsKey(eggs) && map.get(eggs).containsKey(floors))
return map.get(eggs).get(floors);
int min = Integer.MAX_VALUE;
//try dropping at every floor
for(int i = 1; i <= floors; i++){
// 1 for current drop, first call for if egg breaks, second call for if egg does not break
int val = 1 + Math.max(dropEggs(eggs - 1, i - 1, map) , dropEggs(eggs, floors - i, map));
min = Math.min(min, val);
}
if(!map.containsKey(eggs))
map.put(eggs, new HashMap<>());
map.get(eggs).put(floors, min);
return min;
}
}