-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKokoEatingBananas.java
More file actions
45 lines (38 loc) · 1.24 KB
/
Copy pathKokoEatingBananas.java
File metadata and controls
45 lines (38 loc) · 1.24 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
package LeetcodePractice;
public class KokoEatingBananas {
public int minEatingSpeed(int[] piles, int h) {
int left = 1, right = getMax(piles);
int result = right;
while (left <= right) {
int mid = left + (right - left) / 2;
int hours = totalHours(piles, mid);
if (hours <= h) {
result = mid;
right = mid - 1;
} else {
left = mid + 1;
}
}
return result;
}
private int getMax(int[] piles) {
int max = 0;
for (int pile : piles) {
max = Math.max(max, pile);
}
return max;
}
private int totalHours(int[] piles, int speed) {
int hours = 0;
for (int pile : piles) {
hours += (pile + speed - 1) / speed;
}
return hours;
}
public static void main(String[] args) {
KokoEatingBananas solver = new KokoEatingBananas();
System.out.println(solver.minEatingSpeed(new int[]{3, 6, 7, 11}, 8));
System.out.println(solver.minEatingSpeed(new int[]{30,11,23,4,20}, 5));
System.out.println(solver.minEatingSpeed(new int[]{30,11,23,4,20}, 6));
}
}