Koko Eating Bananas

Pattern: Binary search on the answer

medium

Guess a speed k and count the hours, ceil(pile / k) for each pile. Faster speeds never take longer, so binary search for the slowest speed that still finishes within h hours.

The problem

Koko has piles of bananas and h hours. Each hour she picks one pile and eats up to k bananas from it. If the pile has fewer, she eats them all and waits for the next hour. Return the smallest whole speed k that lets her finish every pile within h hours. For example, [3, 6, 7, 11] with h = 8 gives 4, and [30, 11, 23, 4, 20] with h = 5 gives 30.

The function name minEatingSpeed matches LeetCode. Use the link above for the exact statement and limits. h is always at least the number of piles.

Think first

  • At speed k, how many hours does one pile of p bananas take?
  • If speed k works, does k + 1 work?
  • What is the largest speed you would ever need?

Hints

    Brute force, and why it fails

    Try k = 1, 2, 3, … until it fits: O(max × n), which is far too slow when piles hold 10⁹ bananas. Binary search takes O(n log max).

    The key insight

    "The minimum speed such that …" is the smallest guess that passes a monotonic check. The check is a simple sum, and binary search finds the boundary.

    Dry run

    30 
    61lo
    72 
    113 

    piles = [3, 6, 7, 11] and h = 8. Speeds from 1 to 11 are possible; 11 finishes in 4 hours.

    Step 1 of 4

    The plan

    lo = 1, hi = max(piles)
    while lo < hi:
        mid = lo + (hi - lo) / 2
        hours = sum of ceil(p / mid) over piles
        if hours <= h: hi = mid
        else: lo = mid + 1
    return lo
    

    Complexity

    Time
    O(n log max)
    Space
    O(1)

    About log₂ max guesses, each checked with one pass.

    Edge cases

    • h equal to the number of piles: she needs one pile per hour, so the answer is the largest pile.
    • One pile of 1000 and h = 3: the answer is 334.
    • Very large h: the answer is 1.
    I've built my logic, show the code

    Java

    class Solution {
        public int minEatingSpeed(int[] piles, int h) {
            int lo = 1, hi = 0;
            for (int p : piles) hi = Math.max(hi, p);   // eating the biggest pile per hour always finishes in time
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (hours(piles, mid) <= h) hi = mid;   // fast enough: try slower
                else lo = mid + 1;
            }
            return lo;
        }
    
        private long hours(int[] piles, int k) {
            long total = 0;
            for (int p : piles) total += (p + (long) k - 1) / k;   // a pile of p takes ceil(p / k) hours
            return total;
        }
    }

    C++

    class Solution {
    public:
        int minEatingSpeed(vector<int>& piles, int h) {
            int lo = 1, hi = *max_element(piles.begin(), piles.end());   // the biggest pile per hour always works
            while (lo < hi) {
                int mid = lo + (hi - lo) / 2;
                if (hours(piles, mid) <= h) hi = mid;   // fast enough: try slower
                else lo = mid + 1;
            }
            return lo;
        }
    
        long long hours(vector<int>& piles, int k) {
            long long total = 0;
            for (int p : piles) total += (p + static_cast<long long>(k) - 1) / k;   // ceil(p / k) hours
            return total;
        }
    };

    Python

    class Solution:
        def minEatingSpeed(self, piles: list[int], h: int) -> int:
            lo, hi = 1, max(piles)                  # the biggest pile per hour always finishes in time
            while lo < hi:
                mid = (lo + hi) // 2
                if sum((p + mid - 1) // mid for p in piles) <= h:
                    hi = mid                        # fast enough: try slower
                else:
                    lo = mid + 1
            return lo

    Common mistakes

    • Using p / k without rounding up.
    • Adding hours in 32 bits, which overflows for slow speeds and huge piles.
    • Starting the search at 0.

    Similar problems