Koko Eating Bananas
Find the slowest speed that clears every pile in time
DSA, Medium, Binary Search
Which structure or algorithm, and what does it cost.
This is the problem many know as Koko eating bananas, told here with a courier and parcels. A courier must clear piles of parcels from a depot before it closes in h hours. The list piles holds the parcel count of each pile. Each hour the courier picks one pile and unloads at most k parcels from it. An emptied pile ends that hour, even if the courier could unload more. Return the smallest whole number k that clears every pile within h hours.
Examples
Constraints
- 1 <= number of piles <= 100,000.
- Each pile holds between 1 and 100,000,000 parcels.
- The number of piles <=
h<= 1,000,000,000, so an answer always exists. - Trying every speed one by one is not accepted.
Your answer must contain
- The simple method and why it is too slow; the better method; code; time and memory cost; a hand trace of example 1.
Target Aim for O(n log m) time and O(1) memory, where n is the number of piles and m is the largest pile.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
If you knew the speed, how would you count the hours needed? Does a higher speed ever need more hours?
Hint 2 the approach
A higher speed never needs more hours. So the speeds that work form a block from some value upward. You want the first speed in that block.
Hint 3 the key step
The answer lies between 1 and the largest pile. Test a middle speed. If it works, keep it and search lower. If not, search higher.
Walkthrough
Full walkthrough brute force to best
Brute force
Try speeds 1, 2, 3 and so on. For each speed, add up the hours for every pile, and stop at the first speed that fits in h hours. That is O(n * m) time, where m is the largest pile. With 100,000 piles of up to 100 million parcels, that is about 10^13 steps.
Best method
The hours needed never grow as the speed rises, so the speeds that work form a block from some value up to the largest pile. The largest pile always works, because every pile then takes one hour and h is at least the number of piles. Binary search the smallest working speed between 1 and the largest pile. For a middle speed, the hours are the sum over all piles of the pile divided by the speed, rounded up. If the hours are at most h, the speed works, so keep it as a candidate and search lower. Otherwise search higher. This is binary search on the answer: you do not search a list, you search the values the answer can take.
Hand trace
Run on piles [4, 9, 7] with h = 5. The speeds run from 1 to 9. Middle 5: the hours are 1 + 2 + 2 = 5, which fits, so search 1 to 5. Middle 3: the hours are 2 + 3 + 3 = 8, which is too many, so search 4 to 5. Middle 4: the hours are 1 + 3 + 2 = 6, which is too many, so search 5 to 5. One value is left, so the answer is 5.
Code
def min_unload_speed(piles, h):
lo, hi = 1, max(piles) # the answer is in lo..hi
while lo < hi:
mid = (lo + hi) // 2
hours = sum((p + mid - 1) // mid for p in piles) # each pile rounded up
if hours <= h:
hi = mid # mid works: keep it, try lower
else:
lo = mid + 1 # mid is too slow
return loTime and memory
O(n log m) time: each check reads all n piles, and the search makes about log2(m) checks, up to 27 for 100 million. O(1) memory.
Common mistakes
- Rounding the hours down. A pile of 9 at speed 4 needs 3 hours, not 2. Round up with
(p + speed - 1) // speed. - Starting the speed at 0. A speed of 0 divides by zero and can never finish. Start at 1.
- Returning the first speed that works without searching lower. A working speed is the answer only if the speed one below it fails.