We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents f2e8ed5 + 003eede commit 0b78d76Copy full SHA for 0b78d76
LeetCode/875. Koko Eating Bananas.kt
@@ -0,0 +1,27 @@
1
+class Solution {
2
+ fun minEatingSpeed(piles: IntArray, h: Int): Int {
3
+ var min = 1
4
+ var max = piles.max()
5
+ var res = Int.MAX_VALUE
6
+
7
+ fun canFinish(eatNum: Int): Boolean {
8
+ var timeLeft = h
9
+ piles.forEach { pile ->
10
+ timeLeft -= pile / eatNum
11
+ if (pile % eatNum != 0) timeLeft-- //Koko still spends a full hour eating leftover bananas
12
+ }
13
+ return timeLeft >= 0
14
15
16
+ while (min <= max) {
17
+ val mid = (max + min) / 2
18
+ if (canFinish(mid)) { //if such eating rate is able to finish all piles in time, search for the left side
19
+ res = minOf(res, mid)
20
+ max = mid - 1
21
+ } else { //if not, search for the right side
22
+ min = mid + 1
23
24
25
+ return res
26
27
+}
0 commit comments