LeetCode #875 Medium

Koko Eating Bananas

Koko Eating Bananas is LeetCode 875 (Medium). piles[i] is a pile of bananas, and Koko has h hours. She picks one speed k, bananas per hour. Each hour she eats k bananas from one pile; if the pile has fewer than k, she finishes it and waits out the rest of that hour. Return the smallest k that lets her eat every pile within h hours.

  • A pile of p bananas takes ⌈p / k⌉ hours, and piles are never shared within an hour.
  • Piles and h go up to 10⁹, so trying every speed one by one is far too slow.
Constraints
  • 1 <= piles.length <= 10⁴
  • piles.length <= h <= 10⁹
  • 1 <= piles[i] <= 10⁹
binary-searcharraybinary-search-on-answer
Open on LeetCode ↗
02

Intuition

Eating faster never makes the total time longer: each pile's ⌈p / k⌉ can only stay the same or shrink as k grows. So the speeds split into two blocks, too slow on the left and fast enough on the right, and the answer is the first speed of the right block.

Binary search over speeds, not over the array. Try the middle speed and add up the hours. Fast enough means the answer is this speed or slower, so keep it and search left. Too slow means every slower speed fails too, so search right of it. Each try costs one pass over the piles.

How to spot this pattern

"Find the smallest value that works" where a bigger value never stops working is binary search on the answer. The array is not sorted, and it does not need to be: what is sorted is the yes/no answer as the speed rises.

03

Approach

Try it first

Before reading on, take piles = [3, 6, 7, 11] and h = 8. Work out the hours at speeds 3, 4 and 6. Which is the slowest that fits in 8 hours, and why can you skip every speed above 11?

1

Set the search range to 1 through the largest pile

Speed 1 is the slowest possible. At the largest pile's size, every pile takes exactly one hour, so the total is len(piles), which is at most h; a faster speed would not save any time. The answer is somewhere in that range.

2

Count the hours at the middle speed

For a speed k, add up ⌈p / k⌉ over all piles, written (p + k - 1) // k in integer arithmetic. Plain p // k drops the partly eaten last hour of every pile and makes slow speeds look fast enough.

3

Fast enough: keep this speed and look slower

If the total is at most h, this speed works, but a slower one might too. Set high = speed, not speed - 1, so the working speed stays inside the range in case it turns out to be the slowest one.

4

Too slow: look faster

If the total is more than h, this speed fails, and so does every slower speed, since slower never means fewer hours. Set low = speed + 1.

5

Stop when the range is one speed

Each round shrinks the range and never removes the answer, so when low equals high that single speed is the slowest one that finishes in time. Return it.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def minEatingSpeed(self, piles: List[int], h: int) -> int:
▶3 low, high = 1, max(piles)
▶4 while low < high:
▶5 speed = (low + high) // 2
▶6 hours = sum((pile + speed - 1) // speed for pile in piles)
▶7 if hours <= h:
▶8 high = speed
▶9 else:
▶10 low = speed + 1
▶11 return low
speeds111111pileshours36711total8 h allowedthe answer is a speed from 1 to 11
low1
high11
Speed 1 is the slowest possible. At 11 bananas an hour every pile takes one hour, 4 hours in total, which fits in 8; going faster cannot save time. So the answer lies in the blue range.
speeds111111try 6pileshours31 h61 h72 h112 htotal6 h≤8 h allowedspeed 6: 6 hours, fits in 8 → fast enough
round1
speed6
hours6
Try the middle speed, 6. Each pile takes its size divided by 6, rounded up: 7 bananas need 2 hours, because the leftover 1 still uses a whole hour. The total is 6, within the 8 allowed.
speeds11116pileshours36711total8 h allowedkeep 6, look slower: 1 to 6
low1
high6
Speed 6 works, but a slower one might too. High becomes 6, not 5, so this working speed stays in the range in case it is the slowest.
speeds11116try 3pileshours31 h62 h73 h114 htotal10 h>8 h allowedspeed 3: 10 hours, more than 8 → too slow
round2
speed3
hours10
Try the middle speed, 3. Each pile takes its size divided by 3, rounded up: 7 bananas need 3 hours, because the leftover 1 still uses a whole hour. The total is 10, more than the 8 allowed.
speeds11146pileshours36711total8 h alloweddrop 1 to 3, look faster: 4 to 6
low4
high6
Speed 3 is too slow, and anything slower takes at least as long, so every speed from 1 to 3 is ruled out. Low becomes 4.
speeds11146try 5pileshours31 h62 h72 h113 htotal8 h≤8 h allowedspeed 5: 8 hours, fits in 8 → fast enough
round3
speed5
hours8
Try the middle speed, 5. Each pile takes its size divided by 5, rounded up: 6 bananas need 2 hours, because the leftover 1 still uses a whole hour. The total is 8, within the 8 allowed.
speeds11145pileshours36711total8 h allowedkeep 5, look slower: 4 to 5
low4
high5
Speed 5 works, but a slower one might too. High becomes 5, not 4, so this working speed stays in the range in case it is the slowest.
speeds11145try 4pileshours31 h62 h72 h113 htotal8 h≤8 h allowedspeed 4: 8 hours, fits in 8 → fast enough
round4
speed4
hours8
Try the middle speed, 4. Each pile takes its size divided by 4, rounded up: 6 bananas need 2 hours, because the leftover 2 still uses a whole hour. The total is 8, within the 8 allowed.
speeds1114pileshours36711total8 h allowedkeep 4: only 4 is left
low4
high4
Speed 4 works, but a slower one might too. High becomes 4, not 3, so this working speed stays in the range in case it is the slowest.
speeds1114pileshours36711total8 h allowedanswer: 4 bananas an hour
rounds4
answer4
Low and high meet at 4. Every speed below it was shown too slow, and 4 itself was kept because it fits, so it is the slowest speed that finishes in 8 hours. That took 4 tries instead of 11.
05

Common pitfalls

Rounding the hours down

✗ Wrong
hours = sum(pile // speed for pile in piles)
✓ Right
hours = sum((pile + speed - 1) // speed for pile in piles)

A pile of 7 at speed 3 takes 3 hours, not 2: the last banana still uses up an hour. With rounding down, piles = [3, 6, 7, 11], h = 8 returns 3, but speed 3 really needs 10 hours; the answer is 4.

Throwing away a speed that works

✗ Wrong
if hours <= h:
    high = speed - 1
✓ Right
if hours <= h:
    high = speed

With while low < high, cutting below a working speed can lose the answer. piles = [30, 11, 23, 4, 20], h = 6 returns 22, which needs 7 hours; the right answer is 23. If you prefer high = speed - 1, record the speed in a separate answer variable first.

06

Edge cases

h equals the number of piles

Every pile must go in one hour, so only the largest pile's size works, and the search narrows to max(piles).

h is huge compared with the bananas

Speed 1 already fits, every try is fast enough, and high slides down to 1.

07

Complexity

Time
O(n log m)
Space
O(1)
n is the number of piles and m the largest pile. The range 1 to m halves each round, so there are about log₂ m rounds (at most 30 for 10⁹), and each round adds up n pile times.
08

Koko Eating Bananas FAQ

Why not try every speed from 1 upward?

It gives the right answer, since the first speed that fits is the slowest. But a single pile of 10⁹ bananas with h = 1 needs 10⁹ tries, each a pass over the piles. Binary search reaches the same speed in about 30 tries.

Can the search start higher than 1?

Yes. Koko eats at most k bananas per hour, so in h hours she eats at most k × h; she needs k ≥ ⌈sum(piles) / h⌉. Starting low there is safe and trims a few rounds, though the complexity stays O(n log m).

Which other problems use the same search?

Any "smallest value that still works" question where working is all-or-nothing past some point, such as Capacity to Ship Packages Within D Days, which searches a ship capacity with the same keep-or-move-right rule.