Koko Eating Bananas
Every speed is either fast enough or too slow, and once a speed is fast enough, every faster speed is too. So the speeds 1 … max look like ✗✗✗✗✓✓✓✓. Binary search for the first ✓: test the middle speed, and throw away the half that can't hold the answer.
Speed k is fast enough when Σ ceil(p / k) ≤ h. A faster speed never needs more hours, so the answers look like ✗✗✗✓✓✓ and we search for the first ✓.
Problem
Koko has n piles of bananas. Pile i has piles[i] bananas. The guards come back in h hours.
Koko picks one eating speed k (bananas per hour) and keeps it. Every hour she picks one pile and eats k bananas from it. If the pile has fewer than k left, she finishes it and waits for the rest of that hour. She never moves to another pile in the same hour.
Return the smallest integer speed k that lets her finish every pile within h hours.
An integer array piles and an integer h.
One integer: the minimum eating speed.
At speed 4 the piles take 1 + 2 + 2 + 3 = 8 hours, which fits. At speed 3 they take 1 + 2 + 3 + 4 = 10 hours, which is too many.
There are 5 piles and only 5 hours, so every pile must be finished in one hour. That needs speed 30, the biggest pile.
At speed 23 the piles take 2 + 1 + 1 + 1 + 1 = 6 hours. At speed 22 the 23-pile also needs 2 hours, so the total is 7.
- • 1 ≤ piles.length ≤ 10^4
- • piles.length ≤ h ≤ 10^9
- • 1 ≤ piles[i] ≤ 10^9
- • The total hours at a slow speed can reach 10^13 — use a 64-bit type for it.
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
If I tell you a speed, can you quickly work out how many hours Koko needs?
- 2.
How many hours does one pile of
pbananas take at speedk? Why is it not justp / k? - 3.
What is the slowest speed that could make sense? What is a speed that is always fast enough?
- 4.
If speed 5 is fast enough, is speed 6 also fast enough? If speed 5 is too slow, what about speed 4?
- 5.
Do I need to try every speed one by one, or can one check rule out many speeds at once?
Brute force approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Try speed 1, 2, 3, … and return the first one that finishes in time | O(M · N) | O(1) |
| Optimal | Binary search the speed range [1, max]; test each guess with the hours formula | O(N log M) | O(1) |
Why does this belong to this pattern?
- “smallest speed k such that she finishes in time”"Minimum X such that something is possible" → binary search on the answer X.
- “if a pile has fewer than k bananas, she waits”Each pile costs ceil(p / k) hours. Given k, the total time is easy to compute → cheap feasibility check.
- “eating faster never hurts”If k works, k + 1 works. The check is monotonic, so the yes/no answers form ✗✗✗✓✓✓.
This is the anchor problem for searching the answer space. Nothing in the input is sorted, and we never search the piles array. We search the range of possible speeds, and the check is a simple formula: Σ ceil(p / k) ≤ h. The next problems keep the same shape but change the check: a greedy fill (Ship Packages, Split Array) or counting runs of neighbours (Bouquets).
Don't make this mistake
The answer is a speed, not a pile. Example 1's answer is 4, which is not even in [3, 6, 7, 11]. We search the range of possible answers, and use the piles only inside the check.
Speed 0 makes no sense, and if mid ever becomes 0, p / mid divides by zero and the program crashes. The slowest real speed is 1.
A pile of 7 at speed 4 takes 2 hours (4, then 3), but 7 / 4 is 1. Integer division rounds down, so you would think slow speeds are fast enough and return an answer that is too small.
At speed 1, 10^4 piles of 10^9 bananas need 10^13 hours, far past the int limit (about 2.1·10^9). Even p + k - 1 alone can overflow when both are near 10^9.
With lo <= hi, when lo == hi == mid and mid works, hi = mid changes nothing and the loop never ends. Pick one style: lo < hi with hi = mid, or lo <= hi with hi = mid − 1 and a separate ans variable.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Binary Search translate into production architectures and low-level memory mechanics.