Skip to content

Commit 0b78d76

Browse files
committed
2 parents f2e8ed5 + 003eede commit 0b78d76

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)