-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 137.java
More file actions
36 lines (28 loc) · 741 Bytes
/
Day 137.java
File metadata and controls
36 lines (28 loc) · 741 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
class Solution {
public int kokoEat(int[] arr, int k) {
int low = 1;
int high = 0;
for (int bananas : arr) {
high = Math.max(high, bananas);
}
int ans = high;
while (low <= high) {
int mid = low + (high - low) / 2;
if (canFinish(arr, k, mid)) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
return ans;
}
private boolean canFinish(int[] arr, int k, int s) {
long hours = 0;
for (int bananas : arr) {
hours += (bananas + s - 1) / s;
if (hours > k) return false;
}
return true;
}
}