Koko Eating Bananas
01 · Problem
Koko loves to eat bananas. There are n piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in h hours.
Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses some pile of bananas and eats k bananas from that pile. If the pile has less than k bananas, she eats all of them instead and will not eat any more bananas during this hour.
Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.
Return the minimum integer k such that she can eat all the bananas within h hours.
02 · Examples
piles = [3,6,7,11], h = 8
4
At speed k=4, Koko takes ceil(3/4)+ceil(6/4)+ceil(7/4)+ceil(11/4) = 1+2+2+3 = 8 hours.
piles = [30,11,23,4,20], h = 5
30
At speed k=30, each pile takes 1 hour, totaling 5 hours.
piles = [30,11,23,4,20], h = 6
23
At speed k=23, the hours are ceil(30/23)+ceil(11/23)+ceil(23/23)+ceil(4/23)+ceil(20/23) = 2+1+1+1+1 = 6.
03 · Constraints
- 011 <= piles.length <= 104
- 02piles.length <= h <= 109
- 031 <= piles[i] <= 109
04 · Optimal complexity
- Time
- O(n * log(max(piles)))
- Space
- O(1)
Practice it alone or rehearse it as an interview.
Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.