#875
Koko Eating Bananas
medium· Binary Searchruns: 0Koko loves to eat bananas. There are n piles of bananas; the ith pile has piles[i] bananas. The guards will return in h hours. Each hour Koko picks a pile and eats k bananas; if the pile has fewer than k, she eats all of it and waits. Return the minimum integer k such that she can finish all the bananas within h hours.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 369
import math
class Solution:
def minEatingSpeed(self, piles: list[int], h: int) -> int:
left, right = 1, max(piles)
while left < right:
k = (left + right) // 2
hours = sum(math.ceil(p / k) for p in piles)
if hours <= h:
right = k
else:
left = k + 1
return left
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.