forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEggDropping.java
More file actions
37 lines (35 loc) · 1012 Bytes
/
EggDropping.java
File metadata and controls
37 lines (35 loc) · 1012 Bytes
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
import java.lang.*;
import java.io.*;
class Solution {
public static void main (String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-->0){
String[] s = br.readLine().split(" ");
int n = Integer.parseInt(s[0]);
int k = Integer.parseInt(s[1]);
System.out.println(eggDropping(n, k));
}
}
static int eggDropping(int n, int k){
int[][] table = new int[n+1][k+1];
for (int i=1; i<=n; i++){
table[i][1] = 1;
}
for (int i=1; i<=k; i++){
table[1][i] = i;
}
int res = 0;
for (int i=2; i<=n; i++){
for (int j=2; j<=k; j++){
table[i][j] = Integer.MAX_VALUE;
for (int x=1; x<=j; x++){
res = 1+Math.max(table[i][j-x], table[i-1][x-1]);
if (res<table[i][j])
table[i][j] = res;
}
}
}
return table[n][k];
}
}