-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoko eating bananas
75 lines (58 loc) · 1.7 KB
/
Koko eating bananas
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//Koko eating bananas
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while(t-- > 0) {
String line = br.readLine();
String[] tokens = line.split(" ");
ArrayList<Integer> array = new ArrayList<>();
for(String token : tokens) {
array.add(Integer.parseInt(token));
}
int[] arr = new int[array.size()];
int idx = 0;
for(int i : array) {
arr[idx++] = i;
}
int k = Integer.parseInt(br.readLine());
Solution ob = new Solution();
int ans = ob.kokoEat(arr, k);
System.out.println(ans);
}
}
}
class Solution {
public static int kokoEat(int[] arr, int k) {
int mx = arr[0];
for(int ele: arr) {
mx = Math.max(mx, ele);
}
int lo = 1;
int hi = mx;
int res = mx;
while(lo <= hi) {
int mid = lo + (hi - lo) / 2;
if(check(arr, mid, k)) {
hi = mid - 1;
res = mid;
}
else {
lo = mid + 1;
}
}
return res;
}
static boolean check(int[] arr, int mid, int k) {
int hours = 0;
for(int i = 0; i < arr.length; i++) {
hours += arr[i] / mid;
if(arr[i] % mid != 0) {
hours++;
}
}
return hours <= k;
}
}