Koko Eating Bananas

medium

Koko the monkey is alone with n piles of bananas — the i-th pile holds piles[i] bananas. The zookeepers have stepped out and will be back in h hours.

Koko picks a fixed eating speed of k bananas per hour. Each hour she chooses one pile and eats k bananas from it. If fewer than k bananas remain in that pile, she finishes the pile and eats nothing more that hour — she never touches a second pile within the same hour.

Koko likes eating slowly, but every banana must be gone before the keepers return.

Return the minimum integer speed k that lets Koko finish all the bananas within h hours.

Hints

You are not searching the array — you are searching for a speed. What is the smallest speed that could ever work, and the largest speed that is ever needed?
For a fixed speed k, one pass computes the hours Koko needs: each pile costs ceil(piles[i] / k) hours. Can you decide feasibility in O(n)?
If speed k finishes in time, every faster speed does too — a monotone NO…NO YES…YES split. Binary search the smallest YES in [1, max(piles)].

Common doubts

At speed max(piles) every pile takes exactly one hour, and the constraints guarantee h >= piles.length — so that speed always finishes in time. Eating faster cannot help: Koko still spends a full hour per pile.
Hours come in whole units. A pile of 7 at speed 3 takes 3 hours (3 + 3 + 1), not 2. In integer math, round up with (p + k - 1) // k.
No. Each hour she commits to a single pile; if she empties it early, the rest of the hour is wasted. That rule is what makes each pile's cost independent and the total a simple sum.
It never searches the array — it searches the range of speeds 1..max(piles), which is implicitly sorted. Feasibility is monotone over that range, and that is all binary search needs.

Interview follow-ups

No speed works — even infinitely fast, Koko spends one hour per pile. Pointing out that the guarantee h >= piles.length is what makes an answer exist is a strong senior signal.
Yes — ceil(total_bananas / h) is a valid lower bound, since h hours at speed k eat at most k*h bananas. It shrinks the window, though the log factor already makes the search essentially free.
Any problem with a monotone feasibility check over a numeric answer: shipping packages within D days, splitting an array to minimize the largest sum, allocating pages to students, placing aggressive cows. The template is identical — binary search the answer, verify greedily in O(n).

Fun facts

  • This problem is the poster child of a pattern called binary search on the answer: instead of locating an index in an array, you locate a value in the answer space, using a monotone yes/no test as your compass.
  • Roughly 30 feasibility checks suffice for any bound up to 10^9 — doubling the input range costs exactly one extra check, which is why the pattern scales so gracefully.

Asked at

AmazonGoogleMetaBloombergUber
Frequently Sometimes Occasionally
Example 1
Input: piles = [3,6,7,11], h = 8
Output: 4
At speed 4 the piles take 1 + 2 + 2 + 3 = 8 hours — exactly on time. Speed 3 needs 10 hours, too slow.
Example 2
Input: piles = [30,11,23,4,20], h = 5
Output: 30
Five piles, five hours — one hour per pile, so k must swallow the biggest pile whole: 30.
Example 3
Input: piles = [30,11,23,4,20], h = 6
Output: 23
One spare hour lets Koko split only the largest pile across two hours: at speed 23 the costs are 2 + 1 + 1 + 1 + 1 = 6 hours.
Constraints

- 1 <= piles.length <= 10^4 - piles.length <= h <= 10^9 - 1 <= piles[i] <= 10^9

Solve this problem →