-
Notifications
You must be signed in to change notification settings - Fork 819
/
KokoEatingBananas.java
75 lines (72 loc) · 2.04 KB
/
KokoEatingBananas.java
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
package binary_search;
/**
* Created by gouthamvidyapradhan on 23/08/2019 Koko loves to eat bananas. There are N piles of
* bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
*
* <p>Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of
* bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of
* them instead, and won't eat any more bananas during this hour.
*
* <p>Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards
* come back.
*
* <p>Return the minimum integer K such that she can eat all the bananas within H hours.
*
* <p>Example 1:
*
* <p>Input: piles = [3,6,7,11], H = 8 Output: 4 Example 2:
*
* <p>Input: piles = [30,11,23,4,20], H = 5 Output: 30 Example 3:
*
* <p>Input: piles = [30,11,23,4,20], H = 6 Output: 23
*
* <p>Note:
*
* <p>1 <= piles.length <= 10^4 piles.length <= H <= 10^9 1 <= piles[i] <= 10^9
*
* <p>Solution: O(N x log Max(piles[i])) Binary search for the minimum possible value between (1 and
* max(piles[i]))
*/
public class KokoEatingBananas {
public static void main(String[] args) {
int[] A = {312884470};
System.out.println(new KokoEatingBananas().minEatingSpeed(A, 968709470));
}
public int minEatingSpeed(int[] piles, int H) {
int max = 0;
for (int i = 0; i < piles.length; i++) {
max = Math.max(max, piles[i]);
}
if (H == piles.length) return max;
int h = max, l = 1;
int answer = H;
while (l <= h) {
int m = l + (h - l) / 2;
boolean status = check(piles, H, m);
if (status) {
answer = m;
h = m - 1;
} else {
l = m + 1;
}
}
return answer;
}
private boolean check(int[] piles, int H, int k) {
for (int p : piles) {
if (p <= k) {
H--;
} else {
int q = p / k;
if ((p % k) > 0) {
q++;
}
H -= q;
}
if (H < 0) {
return false;
}
}
return true;
}
}