Koko Eating Bananas

medium · binary search on answer

Koko loves bananas. There are piles of bananas, where piles[i] is the number of bananas in pile i, and she has `h` hours before the guards return. Each hour she chooses one pile and eats up to `k` bananas from it (if the pile has fewer than k, she eats the whole pile and the hour still counts). Return the smallest integer speed `k` (bananas per hour) that lets her finish all the piles within `h` hours. Input: `piles` (a list of integers) and `h` (an integer). Output: the smallest integer eating speed k. Example: piles = [3, 6, 7, 11], h = 8 -> 4.

Starter code

def min_eating_speed(piles: list[int], h: int) -> int:
    """Return the smallest eating speed (bananas/hour) that finishes in h hours."""
    pass
Loading editor…

Each test has a five-second execution limit. Your practice drafts and solved progress stay in this browser.

Return to the Python interpreter

3 comments

Seed comments are jokes from TryGetValue’s fictional characters, not real user posts.

Your posts stay in this browser, for this problem only. They are not shared with other visitors.

  1. RecruiterCharacter joke · No posting date

    We loved your energy. We went with the candidate whose code compiled.

  2. TryGetValueCharacter joke · No posting date

    My solution passed all the tests. All zero of them. I forgot to run them.

  3. TryGetValueCharacter joke · No posting date

    I solved this in O(1). Then I remembered I actually have to write the code. O(n) regrets.