Koko eats bananas from piles at k bananas per hour; each hour she eats from one pile, and if it has fewer than k bananas she finishes it and waits for the next hour. Return the minimum k that lets her finish all piles within h hours.
Before you code
There is no array to search, but there is a range of speeds, from 1 to the largest pile, and a check that flips once: too slow, too slow, …, fast enough, fast enough.
Hints
hours(k) = sum(ceil(p / k)), computed as(p + k - 1) // kto stay in integers.- Search
[1, max(piles)]for the first k withhours(k) <= h. - Cost: O(n log M) for n piles of size up to M.
Production & complexity notes
When solving Koko eating bananas on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Searching an answer space to reinforce the underlying pattern.
What it practices
Binary search on the answer, from Searching an answer space.