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.
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.7 at speed 3 takes 3 hours (3 + 3 + 1), not 2. In integer math, round up with (p + k - 1) // k.1..max(piles), which is implicitly sorted. Feasibility is monotone over that range, and that is all binary search needs.h >= piles.length is what makes an answer exist is a strong senior signal.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.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.
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.
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.
- 1 <= piles.length <= 10^4 - piles.length <= h <= 10^9 - 1 <= piles[i] <= 10^9
A deadline, a dial to set, and a yes/no question — Koko Eating Bananas is the cleanest introduction to binary search on the answer, a pattern that turns find the smallest value that works problems into a 30-step search. Master it here and a whole family of problems falls open.
In plain English: pick an integer speed k. Each hour Koko eats from one pile only — k bananas, or the whole pile if fewer remain, wasting the rest of that hour. Pile i therefore costs exactly ceil(piles[i] / k) hours, independent of every other pile. Find the minimum k such that the total hours are at most h.
Worked example — piles = [3,6,7,11], h = 8
speed k = 4: pile 3 → 1 hour (eats 3, hour over) pile 6 → 2 hours (4, then 2) pile 7 → 2 hours (4, then 3) pile 11 → 3 hours (4, 4, then 3) total 1+2+2+3 = 8 ≤ 8 ✓ finishes in time speed k = 3: 1+2+3+4 = 10 > 8 ✗ too slow answer: 4
Two or three sharp questions before coding show you probe the rules instead of assuming them.
“If Koko finishes a pile mid-hour, can she start the next pile in the same hour?”
No — the leftover time is wasted. This is exactly why each pile independently costs ceil of piles[i] / k hours, which makes the feasibility check a simple sum.
“Is the speed k required to be an integer?”
Yes. That makes the answer space the discrete range 1 to max(piles), which binary search can walk in about 30 steps.
“Is h always at least the number of piles?”
Yes — the constraints guarantee it. Each hour covers at most one pile, so with fewer hours than piles no speed, however large, could ever finish.
“Can a pile be empty?”
No, every pile holds at least one banana, so the minimum useful speed is 1 and division by zero never threatens the ceiling formula.
“How big can the pile sizes and h get?”
Both reach 10^9. Any approach that tries speeds one by one is dead on arrival; the number of candidate speeds must shrink exponentially.
Before I code, let me pin down the rules.
Each hour Koko commits to a single pile and any leftover time that hour is wasted — so pile i costs the ceiling of piles[i] divided by k hours, correct?
And since h is at least the number of piles, speed max(piles) always finishes in time — so an answer always exists between 1 and max(piles).
Fix a speed k. Pile i costs ceil(piles[i] / k) hours, so hours(k) = Σ ceil(piles[i] / k). Speed k is feasible when hours(k) <= h. The whole problem collapses to: find the smallest feasible k.
If Koko finishes in time at speed k, she certainly finishes at k + 1. So the answer space splits into a block of NOs followed by a block of YESes:
k: 1 2 3 4 5 6 ... 11
feasible: ✗ ✗ ✗ ✓ ✓ ✓ ... ✓
↑
first YES = answerThe first YES in a NO…NO YES…YES sequence is exactly what binary search finds. Search [1, max(piles)] — the top end is always feasible because h >= piles.length means one hour per pile suffices. That is log2(10^9) ≈ 30 checks of O(n) each: about 3 × 10^5 operations instead of 10^13.
| Brute force | Optimal | |
|---|---|---|
| Time | O(n · max(piles)) | O(n log max(piles)) |
| Space | O(1) | O(1) |
Full code for both approaches lives in the Approaches selector below.
Key takeaway
When a problem asks for the minimum (or maximum) value passing a monotone can-we-do-it test, do not search the data — binary search the answer space and pair it with a linear feasibility check.
lo = 1, hi = max(piles)
while lo < hi:
mid = (lo + hi) / 2
if sum(ceil(p / mid) for p in piles) <= h:
hi = mid # mid works — try slower
else:
lo = mid + 1 # too slow — speed up
return lo