Koko loves to eat bananas. There are
n
piles of bananas, theith
pile haspiles[i]
bananas. The guards have gone and will come back inh
hours.Koko can decide her bananas-per-hour eating speed of
k
. Each hour, she chooses some pile of bananas and eatsk
bananas from that pile. If the pile has less thank
bananas, she eats all of them instead and will not eat any more bananas during this hour.Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return the minimum integer
k
such that she can eat all the bananas withinh
hours.
class Solution:
def minEatingSpeed(self, piles, h):
left, right = 1, max(piles)
while left <= right:
hours = 0
mid = (left + right) // 2
# 测试当前速度 (mid) 需要多少小时
for i in piles:
if i % mid != 0:
hours += i // mid + 1
else:
hours += i // mid
if hours <= h:
right = mid - 1
else:
left = mid + 1
return left