Skip to content
Problem 875 · LeetCode 875(opens in a new tab)

Koko Eating Bananas

MediumArrays

Koko the monkey has some piles of bananas, where piles[i] is the size of pile i. She has h hours before the guards are back, and she picks one eating speed k, in bananas per hour, for the whole time.

In each hour she goes to one pile and eats k bananas from it. If that pile has fewer than k left, she finishes it and waits out the rest of the hour without starting another pile.

Return the smallest whole-number speed k that lets her clear every pile within h hours.

Example 1

Input:  piles = [4, 9, 6], h = 5
Output: 5
Why:    at speed 5 the piles take 1 + 2 + 2 = 5 hours; speed 4 needs 6
2 more examples and the constraints

Example 2

Input:  piles = [10, 2, 7], h = 3
Output: 10
Why:    one hour per pile, so she must clear the 10 in one go

Example 3

Input:  piles = [12], h = 4
Output: 3
Why:    12 bananas over 4 hours is 3 an hour

Constraints

  • 1 ≤ length of piles ≤ 10,000
  • length of piles ≤ h ≤ 1,000,000,000
  • 1 ≤ piles[i] ≤ 1,000,000,000

Your turn first

Have a go on paper or in your own editor, then reveal the approaches from the simplest up.

Try it on LeetCode(opens in a new tab)
  1. Try every speedO(n · m)
  2. Binary search on the speedO(n log m)

The idea in one sentence

The pattern, how to spot it, and the best complexity.

Side by side

ApproachTimeRelative work for 10,000 piles of up to 1,000,000,000Verdict
Try every speedO(n · m)
Mention it, then move on
Binary search on the speedO(n log m)
Best; about 30 rounds

n = number of piles, m = size of the biggest pile.

Edge cases to test

  • h equals the number of piles. One hour per pile, so the answer is the biggest pile: [10, 2, 7] with h = 3 gives 10.
  • Lots of spare time. [5] with h = 10 gives 1; the speed can never go below 1.
  • Totals that overflow. Three piles of 1,000,000,000 at speed 1 need 3 billion hours, past a 32-bit int.
  • A single pile. [12] with h = 4 is plain division rounded up: 3.

Common mistakes

  • Using p // k instead of rounding up, so a pile of 7 at speed 3 counts as 2 hours, not 3.
  • Writing hi = mid - 1 when mid works, which can skip the answer itself.
  • Summing the hours in a 32-bit int, or computing lo + hi when both are near a billion.

How to talk through it

  1. 1Clarify. Can Koko move to a second pile in the same hour? (No.) Is h always at least the number of piles? (Yes.)
  2. 2Give the brute force. Try speeds 1, 2, 3 and stop at the first that fits. Say why it is slow: a billion possible speeds.
  3. 3Spot the order. A faster speed never needs more hours, so the speeds split into fail, then pass. That is the cue for binary search.
  4. 4Code the search. Search between 1 and the biggest pile; keep mid when it works and skip past it when it does not.
  5. 5Test out loud. Run h equal to the pile count, a single pile, and huge piles for overflow.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?