Binary Search on the Answer: Koko Eating Bananas in Python
Binary search on the answer tests the middle of the possible answers and keeps the half that works. LeetCode 875, Koko Eating Bananas, in Python.
Binary search on the answer is binary search over the possible answers to a question instead of over the items of a list. LeetCode 875, Koko Eating Bananas, is the classic example: there are piles of bananas, Koko eats at a fixed speed of k bananas an hour, she can work on one pile per hour (if a pile has fewer than k left, she finishes it and waits for the next hour), and she has h hours. What is the slowest speed k that still finishes every pile in time?

What does a thermostat have to do with binary search?
Say you want the lowest thermostat setting that still keeps you warm. You don't try 16, 17, 18 and so on. You try the middle setting. If you're warm there, every higher setting is warm too, so the answer is that setting or a lower one: go lower. If you're cold, every lower setting is colder, so the answer is higher.

That works because the answers are ordered and the test flips only once: cold, cold, cold, then warm from some setting on. You aren't searching a list for a value; you're searching the range of answers for the place where the test flips.
How does that solve Koko Eating Bananas?
Take the piles [3, 6, 7, 11] and h = 8 hours. At speed k, a pile of p bananas takes p ÷ k hours, rounded up, so the total time is the sum of that over the piles. The faster Koko eats, the fewer hours she needs, so the test "does speed k finish in 8 hours?" flips once, from no to yes, just like the thermostat.

The possible answers run from 1 to the biggest pile, 11: at speed 11 every pile takes one hour, and no faster speed can do better than one hour a pile.
| k | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| hours | 27 | 15 | 10 | 8 | 8 | 6 | 5 | 5 | 5 | 5 | 4 |
What does it look like in Python?
hours adds up each pile divided by k, rounded up ((p + k - 1) // k is that rounding up in whole numbers). lo and hi mark the speeds still possible. While more than one is left, test the middle speed: fast enough, and the answer is mid or slower, so hi drops to mid; too slow, and the answer is faster, so lo moves past mid. When lo meets hi, that speed is the answer.
This is the program from the video, line for line:
def hours(piles, k):
return sum((p + k - 1) // k for p in piles)
def min_speed(piles, h):
lo, hi = 1, max(piles)
while lo < hi:
mid = (lo + hi) // 2
if hours(piles, mid) <= h:
hi = mid
else:
lo = mid + 1
return lo
print(min_speed([3, 6, 7, 11], 8))
Output:
4
Which speeds does it test?
| Test | lo | hi | mid | hours(mid) | Then |
|---|---|---|---|---|---|
| 1 | 1 | 11 | 6 | 6 | fast enough: hi = 6 |
| 2 | 1 | 6 | 3 | 10 | too slow: lo = 4 |
| 3 | 4 | 6 | 5 | 8 | fast enough: hi = 5 |
| 4 | 4 | 5 | 4 | 8 | fast enough: hi = 4 |

Now lo and hi are both 4, and the loop ends: the slowest speed is 4 bananas an hour. Speed 3 needs 10 hours, so nothing slower than 4 works.

We also checked min_speed against trying every speed from 1 up on 3,000 random sets of piles, and the answers always agree. LeetCode's other two examples, min_speed([30, 11, 23, 4, 20], 5) and min_speed([30, 11, 23, 4, 20], 6), return 30 and 23.
How fast is it?
Each test halves the range of speeds, so with a biggest pile of m there are about log₂ m tests, and each test costs one pass over the n piles: O(n log m) time and O(1) extra memory. Trying every speed would cost O(n · m). The same pattern solves any problem where you can test a guessed answer and the test flips once: shipping capacity, splitting an array, the smallest divisor under a threshold.
Try it
Same piles, [3, 6, 7, 11], but Koko only has 6 hours. What is the slowest speed that still finishes? Use the hours table above, or run min_speed([3, 6, 7, 11], 6).