875. Koko Eating Bananas

Koko loves to eat bananas. There are n piles of bananas, the i t h i^{th} 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 will not eat any more bananas during this hour.

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 1:
Example 2:
Example 3:
Constraints:
  • 1 < = p i l e s . l e n g t h < = 1 0 4 1 <= piles.length <= 10^4 1<=piles.length<=104
  • p i l e s . l e n g t h < = h < = 1 0 9 piles.length <= h <= 10^9 piles.length<=h<=109
  • 1 < = p i l e s [ i ] < = 1 0 9 1 <= piles[i] <= 10^9 1<=piles[i]<=109

From: LeetCode
Link: 875. Koko Eating Bananas


Solution:

Ideas:
  • Left Bound (low): Start with 1 because Koko cannot eat at a speed slower than 1 banana per hour.

  • Right Bound (high): Start with the maximum number of bananas in any pile. This is because Koko does not need to eat faster than the largest pile to finish all bananas within h hours.

  • Binary Search: Calculate the mid-point mid between low and high. For each mid, calculate the total hours Koko takes to eat all bananas at this speed. If the total hours exceed h, Koko needs to eat faster, so we move the low up to mid + 1. Otherwise, we can potentially slow down, so we adjust high to mid.

  • Termination Condition: The search continues until low meets high, which will be the minimum speed Koko can eat all the bananas within h hours.

Caode:
// Helper function to calculate the total hours taken to eat all bananas at speed k
int hoursNeeded(int* piles, int pilesSize, int k) {
    int hours = 0;
    for (int i = 0; i < pilesSize; i++) {
        hours += piles[i] / k;
        if (piles[i] % k != 0) { // If there are leftovers, it takes an extra hour
            hours++;
        }
    }
    return hours;
}

int minEatingSpeed(int* piles, int pilesSize, int h) {
    int low = 1, high = 0;
    // Find the maximum pile to set as the initial high bound
    for (int i = 0; i < pilesSize; i++) {
        if (piles[i] > high) {
            high = piles[i];
        }
    }
    
    while (low < high) {
        int mid = low + (high - low) / 2;
        if (hoursNeeded(piles, pilesSize, mid) > h) {
            low = mid + 1; // Koko needs to eat faster
        } else {
            high = mid; // Koko can afford to eat slower
        }
    }
    
    return low; // low and high converge to the minimum speed
}
02-07 06:55