Koko Eating Bananas
Koko Eating Bananas is LeetCode 875 (Medium). piles[i] is a pile of bananas, and Koko has h hours. She picks one speed k, bananas per hour. Each hour she eats k bananas from one pile; if the pile has fewer than k, she finishes it and waits out the rest of that hour. Return the smallest k that lets her eat every pile within h hours.
- A pile of
pbananas takes ⌈p / k⌉ hours, and piles are never shared within an hour. - Piles and
hgo up to 10⁹, so trying every speed one by one is far too slow.
- 1 <= piles.length <= 10⁴
- piles.length <= h <= 10⁹
- 1 <= piles[i] <= 10⁹
Intuition
Eating faster never makes the total time longer: each pile's ⌈p / k⌉ can only stay the same or shrink as k grows. So the speeds split into two blocks, too slow on the left and fast enough on the right, and the answer is the first speed of the right block.
Binary search over speeds, not over the array. Try the middle speed and add up the hours. Fast enough means the answer is this speed or slower, so keep it and search left. Too slow means every slower speed fails too, so search right of it. Each try costs one pass over the piles.
"Find the smallest value that works" where a bigger value never stops working is binary search on the answer. The array is not sorted, and it does not need to be: what is sorted is the yes/no answer as the speed rises.
Approach
Before reading on, take piles = [3, 6, 7, 11] and h = 8. Work out the hours at speeds 3, 4 and 6. Which is the slowest that fits in 8 hours, and why can you skip every speed above 11?
Set the search range to 1 through the largest pile
Speed 1 is the slowest possible. At the largest pile's size, every pile takes exactly one hour, so the total is len(piles), which is at most h; a faster speed would not save any time. The answer is somewhere in that range.
Count the hours at the middle speed
For a speed k, add up ⌈p / k⌉ over all piles, written (p + k - 1) // k in integer arithmetic. Plain p // k drops the partly eaten last hour of every pile and makes slow speeds look fast enough.
Fast enough: keep this speed and look slower
If the total is at most h, this speed works, but a slower one might too. Set high = speed, not speed - 1, so the working speed stays inside the range in case it turns out to be the slowest one.
Too slow: look faster
If the total is more than h, this speed fails, and so does every slower speed, since slower never means fewer hours. Set low = speed + 1.
Stop when the range is one speed
Each round shrinks the range and never removes the answer, so when low equals high that single speed is the slowest one that finishes in time. Return it.
Code in Python, C++ and Java
Common pitfalls
Rounding the hours down
hours = sum(pile // speed for pile in piles)
hours = sum((pile + speed - 1) // speed for pile in piles)
A pile of 7 at speed 3 takes 3 hours, not 2: the last banana still uses up an hour. With rounding down, piles = [3, 6, 7, 11], h = 8 returns 3, but speed 3 really needs 10 hours; the answer is 4.
Throwing away a speed that works
if hours <= h:
high = speed - 1if hours <= h:
high = speedWith while low < high, cutting below a working speed can lose the answer. piles = [30, 11, 23, 4, 20], h = 6 returns 22, which needs 7 hours; the right answer is 23. If you prefer high = speed - 1, record the speed in a separate answer variable first.
Edge cases
Every pile must go in one hour, so only the largest pile's size works, and the search narrows to max(piles).
Speed 1 already fits, every try is fast enough, and high slides down to 1.
Complexity
Koko Eating Bananas FAQ
Why not try every speed from 1 upward?
It gives the right answer, since the first speed that fits is the slowest. But a single pile of 10⁹ bananas with h = 1 needs 10⁹ tries, each a pass over the piles. Binary search reaches the same speed in about 30 tries.
Can the search start higher than 1?
Yes. Koko eats at most k bananas per hour, so in h hours she eats at most k × h; she needs k ≥ ⌈sum(piles) / h⌉. Starting low there is safe and trims a few rounds, though the complexity stays O(n log m).
Which other problems use the same search?
Any "smallest value that still works" question where working is all-or-nothing past some point, such as Capacity to Ship Packages Within D Days, which searches a ship capacity with the same keep-or-move-right rule.