KatInterview

907. Koko Eating Bananas

MediumSolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks for the minimum integer eating speed k (bananas per hour) that allows Koko to eat all bananas in n piles within h hours. For a given speed k, the time to eat a pile with p bananas is ceil(p / k) hours. The total time for a given speed k is the sum of times for all piles: T(k) = sum(ceil(piles[i] / k)).

Solution

Koko Eating Bananas in Python

class Solution:
    def minEatingSpeed(self, piles: List[int], h: int) -> int:
        # The search space for k is from 1 to max(piles).
        low = 1
        high = max(piles)
        ans = high

        while low <= high:
            # mid can't be 0 as low starts from 1.
            mid = (low + high) // 2
            
            # Calculate total hours with speed 'mid'
            # Python's integers handle arbitrary size, so no overflow issues for total_hours.
            total_hours = 0
            for pile in piles:
                # Using integer arithmetic for ceiling division is generally preferred
                # over math.ceil() for speed and precision.
                total_hours += (pile + mid - 1) // mid

            if total_hours <= h:
                # This speed is feasible. Try for a smaller speed.
                ans = mid
                high = mid - 1
            else:
                # This speed is too slow. Increase the speed.
                low = mid + 1
        
        return ans

Koko Eating Bananas in Java

class Solution {
    public int minEatingSpeed(int[] piles, int h) {
        // The minimum possible speed is 1.
        int low = 1;
        // The maximum reasonable speed is the size of the largest pile.
        int high = 0;
        for (int pile : piles) {
            high = Math.max(high, pile);
        }

        int ans = high;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            
            // Calculate total hours to eat all bananas at speed 'mid'.
            // Use long for total_hours to prevent overflow.
            long total_hours = 0;
            for (int pile : piles) {
                // ceil(pile / mid) can be calculated as (pile + mid - 1) / mid
                total_hours += (long)(pile + mid - 1) / mid;
            }

            if (total_hours <= h) {
                // This speed is possible. Try a smaller speed.
                ans = mid;
                high = mid - 1;
            } else {
                // This speed is too slow. Need to eat faster.
                low = mid + 1;
            }
        }

        return ans;
    }
}

Koko Eating Bananas in C++

class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        // The minimum possible speed is 1.
        int low = 1;
        // The maximum reasonable speed is the size of the largest pile.
        int high = 0;
        for (int pile : piles) {
            high = max(high, pile);
        }

        int ans = high;

        while (low <= high) {
            int mid = low + (high - low) / 2;
            // Safety check although low starts at 1, so mid is always > 0.
            if (mid == 0) {
                low = 1;
                continue;
            }
            
            // Calculate total hours to eat all bananas at speed 'mid'.
            // Use long long for total_hours to prevent overflow.
            long long total_hours = 0;
            for (int pile : piles) {
                // ceil(pile / mid) can be calculated as (pile + mid - 1) / mid
                total_hours += (long long)(pile + mid - 1) / mid;
            }

            if (total_hours <= h) {
                // This speed is possible. Try a smaller speed.
                ans = mid;
                high = mid - 1;
            } else {
                // This speed is too slow. Need to eat faster.
                low = mid + 1;
            }
        }

        return ans;
    }
};

Koko Eating Bananas in TypeScript

function minEatingSpeed(piles: number[], h: number): number {
    // The minimum possible speed is 1.
    let low = 1;
    // The maximum reasonable speed is the size of the largest pile.
    let high = Math.max(...piles);

    let ans = high;

    while (low <= high) {
        const mid = Math.floor(low + (high - low) / 2);

        // mid cannot be 0 as low starts at 1, but as a safety measure.
        if (mid === 0) {
            low = 1;
            continue;
        }

        // Calculate total hours to eat all bananas at speed 'mid'.
        // The sum can be large, but fits within JavaScript's Number.MAX_SAFE_INTEGER.
        let total_hours = 0;
        for (const pile of piles) {
            // ceil(pile / mid) can be calculated using integer arithmetic.
            total_hours += Math.floor((pile + mid - 1) / mid);
        }

        if (total_hours <= h) {
            // This speed is possible. Try a smaller speed.
            ans = mid;
            high = mid - 1;
        } else {
            // This speed is too slow. Need to eat faster.
            low = mid + 1;
        }
    }

    return ans;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.