Skip to content

Commit b3bc443

Browse files
committed
Added 1 solution
1 parent 087ddb0 commit b3bc443

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Medium/Koko Eating Bananas.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public int minEatingSpeed(int[] piles, int H) {
3+
int maxSize = piles[0];
4+
for (int pile : piles) {
5+
maxSize = Math.max(pile, maxSize);
6+
}
7+
return helper(piles, H, 1, maxSize);
8+
}
9+
10+
private int helper(int[] piles, int H, int start, int end) {
11+
while (start < end) {
12+
int mid = (start + end) / 2;
13+
if (isPossible(piles, H, mid)) {
14+
end = mid;
15+
}
16+
else {
17+
start = mid + 1;
18+
}
19+
}
20+
return start;
21+
}
22+
23+
private boolean isPossible(int[] piles, int H, int check) {
24+
int numOfHours = 0;
25+
for (int pile : piles) {
26+
int quot = pile / check;
27+
numOfHours += quot == 0 ? 1 : quot;
28+
numOfHours += quot > 0 && pile % check != 0 ? 1 : 0;
29+
}
30+
return numOfHours <= H;
31+
}
32+
}
33+

0 commit comments

Comments
 (0)