Koko Eating Bananas

Solution For Koko Eating Bananas

Sure! Here’s a detailed solution to the Koko Eating Bananas problem on LeetCode.

Problem Statement:

Koko loves to eat bananas. There are N piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in H hours. Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and starts picking up from the next pile.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

Return the minimum integer K such that she can eat all the bananas within H hours.

Example Input:

Input: piles = [3,6,7,11], H = 8
Output: 4

Solution:

To solve this problem we need to understand that Koko can eat at a certain speed, K bananas per hour, and we need to find the minimum possible value of K that will allow Koko to eat all the bananas within H hours.

We can use binary search to get the minimum value of K.

We know that the minimum and maximum values of K are 1 and max(piles) respectively. Therefore, we need to search in between 1 and max(piles).

We can perform a binary search of all possible K values within the range. At each iteration, we will calculate the time Koko needs to eat all the bananas for a particular value of K. If the time is less than or equal to H, the search is continued on the left side of the mid value. Otherwise, we continue on the right side of the mid value.

Finally, the minimum K will be the answer when the search is complete.

Here’s the python code that implements the above solution.

python
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
left, right = 1, max(piles)
while left < right:
mid = (left + right) // 2
time = sum((pile-1) // mid + 1 for pile in piles)
if time > H:
left = mid + 1
else:
right = mid
return left

In this code, we first initialize the left and right values. Then we check if the left value is lesser than the right value. While left is less than right, we find the mid value.

Then, we calculate the time Koko needs to eat all the bananas at mid speed. We will check if this time is less than H. If the time is less than H, then Koko can eat at a faster speed, so we search for lower value of K. If the time is greater than H, then Koko cannot eat at such an intense speed, so we search for a higher value of K.

Finally we return the minimum speed needed by Koko to eat all the bananas within H hours.

Complexity Analysis:

Time Complexity:

The time complexity of this solution is O(n log W), where n is the number of piles and W is the maximum number of bananas in a pile. We do a binary search of all possible K values within the range, O(logW), and for each K value, we calculate the time Koko needs to eat all the bananas, which takes O(n).

Space Complexity:

The space complexity of this solution is O(1) as we use constant space to store the left, right and mid values and we calculate time on the fly.

Step by Step Implementation For Koko Eating Bananas

class Solution {
    public int minEatingSpeed(int[] piles, int H) {
        // we need to find the minimum speed at which Koko can eat all the bananas in H hours
        // we can use a binary search approach to find the minimum speed
        // we can set the lower bound to be 1 (the slowest speed) and the upper bound to be the max speed
        int low = 1;
        int high = Integer.MAX_VALUE;
        
        // we need to keep track of the number of hours it takes to eat all the bananas at a certain speed
        // we can do this by using a while loop
        while (low < high) {
            // we need to find the midpoint between the lower and upper bounds
            int mid = low + (high - low) / 2;
            // we need to see if it is possible for Koko to eat all the bananas in H hours at speed mid
            if (canEatAll(piles, H, mid)) {
                // if it is possible, we need to try a higher speed
                // this is because we want to find the minimum speed at which Koko can eat all the bananas in H hours
                low = mid + 1;
            } else {
                // if it is not possible, we need to try a lower speed
                high = mid;
            }
        }
        // once the while loop terminates, we know that low is the minimum speed at which Koko can eat all the bananas in H hours
        return low;
    }
    
    // this helper function returns true if it is possible for Koko to eat all the bananas in H hours at speed mid
    // and returns false otherwise
    public boolean canEatAll(int[] piles, int H, int mid) {
        // we need to keep track of the number of hours it takes to eat all the bananas at speed mid
        int hours = 0;
        // we need to go through all the piles of bananas
        for (int pile : piles) {
            // we need to divide the number of bananas in the pile by the speed to find the number of hours it takes to eat them all
            // we need to round up because Koko always eats the next banana if there are any left
            hours += Math.ceil((double) pile / mid);
        }
        // if the number of hours is less than or equal to H, it is possible for Koko to eat all the bananas in H hours
        // and we return true
        // otherwise, it is not possible and we return false
        return hours <= H;
    }
}
There are a few ways to solve this problem. One way would be to use a greedy algorithm.

We can keep track of the amount of time Koko has to eat a banana, and try to minimize the time she spends eating all the bananas.

We can sort the bananas by their size, so that Koko can eat the smaller bananas first. Then, we can keep track of the time she spends eating each banana, and try to minimize the total time.

Another way to solve this problem would be to use a dynamic programming approach.

We can create a 2D array, where the first dimension represents the number of bananas Koko has eaten, and the second dimension represents the time she has taken to eat them.

We can then fill in the array using a bottom-up approach, so that we can calculate the minimum time Koko needs to eat all the bananas.
var kokoBananas = function(piles, H) {
    // keep track of the min and max possible eating speeds
    let min = 1, max = Math.max(...piles);
    
    // as long as there is a possible range of speeds to check
    while (min < max) {
        // set the midpoint as the potential eating speed
        let mid = Math.floor((min + max) / 2);
        // initialize a variable to track the total time it would take to eat all the bananas at that speed
        let time = 0;
        
        // for each pile of bananas
        for (let i = 0; i < piles.length; i++) {
            // add the time it would take to eat that pile at the current speed to the total time
            time += Math.ceil(piles[i] / mid);
        }
        
        // if the total time is less than the time limit, that means we can eat faster and still finish on time
        if (time <= H) {
            // so we update the min possible eating speed to be the current speed
            min = mid + 1;
        // if the total time is more than the time limit, that means we need to eat slower
        } else {
            // so we update the max possible eating speed to be one slower than the current speed
            max = mid;
        }
    }
    
    // once we have finished iterating, the min possible eating speed will be the max possible eating speed, so we return that
    return min - 1;
};
class Solution {
public:
    int minEatingSpeed(vector& piles, int H) {
        int lo = 1, hi = *max_element(piles.begin(), piles.end());
        while (lo <= hi) {
            int mi = lo + (hi - lo) / 2;
            if (canEatAll(piles, H, mi)) hi = mi - 1;
            else lo = mi + 1;
        }
        return lo;
    }

private:
    bool canEatAll(vector& piles, int H, int k) {
        int time = 0;
        for (int pile : piles) 
            time += (pile - 1) / k + 1;
        return time <= H;
    }
};
There are a few ways to solve this problem. One way would be to keep track of the amount of time Koko has to eat each banana, and then keep track of the total time she spends eating all the bananas. Then, we can just return the total time she spends eating all the bananas.

Another way to solve this problem would be to keep track of the index of the banana Koko is currently eating, and the time she spends eating each banana. Then, we can just return the index of the banana she is currently eating.


Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]