Skip to content

Koko Eating Bananas

Find the slowest eating speed that clears every pile within h hours, by binary searching on the speed.

MediumBinary Search
Solve on LeetCode

The problem

There are piles of bananas, with piles[i] bananas in pile i, and the guards return in h hours, where h is at least the number of piles. Each hour, Koko picks one pile and eats up to k bananas from it; if the pile has fewer than k, she finishes it and waits for the next hour. Return the smallest whole number k that lets her finish every pile within h hours.

Input piles = [5, 9, 2], h = 6

Output 3

At 3 bananas an hour she needs 2 + 3 + 1 = 6 hours; at 2 an hour she would need 3 + 5 + 1 = 9.

Input piles = [10, 4], h = 2

Output 10

With one hour per pile, she has to finish the biggest pile, 10, in a single hour.

Key constraint: A pile can hold up to a billion bananas, so trying every speed from 1 upward is far too slow.

How to spot the pattern

  • You are asked for the smallest value (a speed) that makes something possible.
  • If a speed works, every faster speed works too, so the answers form a no-then-yes boundary you can binary search.

The idea

Instead of searching an array, binary search the answer itself. At speed k the hours needed are the sum of ceil(pile / k), and that number never goes up when k goes up. So the speeds split into "too slow" followed by "fast enough", and a binary search over 1 to max(piles) finds the first speed that is fast enough.

Brute force: Try speeds 1, 2, 3 and so on until one fits in h hours, which takes O(n * m) time when the largest pile is m.

Step by step

  1. 1Set lo = 1 and hi = max(piles); speed hi always works, because then each pile takes one hour.
  2. 2While lo < hi, take mid and add up ceil(pile / mid) over all piles.
  3. 3If those hours fit in h, mid is fast enough, so set hi = mid; otherwise set lo = mid + 1.
  4. 4Return lo.

See the pattern move

The binary search pattern on a small example. Step through it, or press play.

Code

class Solution:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        def hours_needed(k: int) -> int:
            return sum((pile + k - 1) // k for pile in piles)  # ceil(pile / k) each

        lo, hi = 1, max(piles)  # speed max(piles) always works
        while lo < hi:
            mid = lo + (hi - lo) // 2
            if hours_needed(mid) <= h:
                hi = mid  # fast enough: try slower speeds
            else:
                lo = mid + 1  # too slow
        return lo

Complexity

Time

O(n log m)

Space

O(1)

About log2(m) guesses, where m is the largest pile, and each guess adds up the hours over all n piles.

Watch out for

  • Forgetting to round up: a pile of 7 at speed 3 takes 3 hours, not 2, so use (pile + k - 1) / k or a ceiling function.
  • Counting hours in a 32-bit int without checking the limits: at slow speeds the count can be huge (10,000 piles of 10^9 bananas take 10^13 hours at speed 1), so the Java and C++ code adds them up in a 64-bit long.
  • Starting lo at 0, which leads to a division by zero.

Follow-up questions

Why is it safe to binary search on the speed?

Because the hours needed never go up when the speed goes up. Any yes/no question with that shape, all no's followed by all yes's, can be binary searched, even when there is no sorted array in sight.

Can you tighten the search range?

Yes: she eats at most k bananas an hour for h hours, so k is at least ceil(sum(piles) / h). Starting lo there saves a few steps, but the complexity stays the same.

In Hinglish

Answer yaani speed par hi binary search karo: har speed se hours nikalo, h mein fit ho jaaye toh aur kam speed try karo, warna zyada.

Related problems