mediumBinary SearchAmazonGoogle
Banana Pacing Rate
A koala eats at some fixed rate k bananas/hour from several piles. Find the slowest rate that finishes within h hours.
Given `piles` (sizes) and `h` (hours), return the minimum integer rate k such that total eating time ≤ h.
Choose approach
3
06
17
211
3Maptrial
empty
1/6
Narrationstep 1 of 6
Search eating rate k ∈ [1, max(piles)=11]. A faster k is always feasible — this is monotonic.
Variables
lo=1
hi=11
h=8
How to think about it
- Faster = fewer hours — monotonic structure on the rate axis.
- Lo = 1 (can't eat 0). Hi = max pile (never need faster).
- Each probe is O(n) to sum ceil(pile/k).
- O(log(max)) probes → O(n log max) overall.
Edge cases to catch in interview
- h = n → must finish each pile in one hour → rate = max pile.
- Very big h → rate = 1.
- One huge pile → rate = ceil(pile / h) effectively.
Examples
Example 1
4{"piles":[3,6,7,11],"h":8,"values":[3,6,7,11]}
Example 2
30{"piles":[30,11,23,4,20],"h":5,"values":[30,11,23,4,20]}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed