Koko Eating Bananas
Medium · Binary Search · Array · Binary Search
Problem
piles[i] bananas sit in pile i, and there are h hours available. At a chosen speed of k bananas per hour, each hour is spent on a single pile: if fewer than k remain there, the rest of the hour is wasted.
Return the smallest integer k that finishes every pile within h hours.
Examples
Input: piles = [3,6,7,11], h = 8 Output: 4
Speed 4 takes 1+2+2+3 = 8 hours; speed 3 would take 10.
Input: piles = [30,11,23,4,20], h = 5 Output: 30
One pile per hour, so the speed must match the largest.
Constraints
- 1 <= len(piles) <= 10^4
- len(piles) <= h <= 10^9
- 1 <= piles[i] <= 10^9
Approaches
- Try every speed upward — time O(n · max pile), space O(1)
Related problems
Start free on Cariara · Coding problems · System design studio