Koko Eating Bananas Optimal Binary Search Approach

Visualization Player

Problem Statement

Koko the monkey loves bananas. She has a list of banana piles, and she wants to eat all of them within h hours. Each hour, Koko chooses any pile and eats up to k bananas from it. If the pile has fewer than k bananas, she eats the entire pile and moves on.

Your task is to help Koko determine the minimum integer eating speed k such that she can finish all the bananas in at most h hours.

If the list of banana piles is empty, return 0 as there are no bananas to eat.

Examples

Banana Piles Hours (h) Minimum Eating Speed (k) Description
[3, 6, 7, 11] 8 4 Koko can eat all piles in 8 hours if she eats 4 bananas/hour
[30, 11, 23, 4, 20] 5 30 She must eat very fast to finish in just 5 hours
[30, 11, 23, 4, 20] 6 23 With 6 hours, speed of 23 is sufficient
[1, 1, 1, 1] 4 1 Minimum speed needed is 1 banana/hour
[1000000000] 2 500000000 Single large pile, she must eat half in each hour
[] 5 0 No piles to eat, so minimum speed is 0
[10] 1 10 Only one pile, must eat all in one hour

Solution

Understanding the Problem

We are given a list of banana piles, and a total number of hours h within which Koko must finish eating all the bananas. Each hour, Koko chooses one pile and eats up to k bananas from it. If the pile has fewer than k bananas, she eats all of them and doesn’t continue to another pile in the same hour.

The goal is to find the minimum integer speed k such that Koko can finish eating all the bananas within h hours. This is essentially an optimization problem — we want the smallest value of k for which the total time taken does not exceed h.

Step-by-Step Solution with Example

Step 1: Understand how time is calculated

If Koko eats at a speed k, then for each pile, the number of hours she needs is ceil(pile / k). We need to compute this for all piles and add it up to find the total time.

Step 2: Choose an example

Let's say the piles are [3, 6, 7, 11] and h = 8. Koko must eat all bananas in at most 8 hours.

Step 3: Identify possible range for k

The minimum speed is 1 banana per hour. The maximum speed is the largest pile, 11, because if she can finish the biggest pile in one hour, she can finish any pile in one hour.

Step 4: Use Binary Search to find the minimum k

We now perform binary search between 1 and 11:

  • Mid = 6: Total time = ceil(3/6) + ceil(6/6) + ceil(7/6) + ceil(11/6) = 1 + 1 + 2 + 2 = 6 (less than 8) → try smaller speed.
  • Mid = 3: Total time = ceil(3/3) + ceil(6/3) + ceil(7/3) + ceil(11/3) = 1 + 2 + 3 + 4 = 10 (too much) → try higher speed.
  • Mid = 4: Total time = 1 + 2 + 2 + 3 = 8 (just fits) → try smaller speed to optimize further.
  • Mid = 3: already tested, doesn’t work → so answer is 4.

Step 5: Return the result

The minimum speed k that allows Koko to finish within h = 8 hours is 4.

Edge Cases

  • Exact Fit: If h equals the number of piles, then Koko must eat one pile per hour. So the speed must be at least as large as the largest pile.
  • Very Large h: If h is very high, Koko can eat slowly. Even speed = 1 might be enough.
  • Only One Pile: Then the speed is just ceil(pile / h).
  • No Piles: If the list is empty, return 0 since there's nothing to eat.
  • All piles are size 1: The total time is equal to the number of piles, and any k ≥ 1 would work if h is enough.

Finally

This problem is a great example of combining simulation with optimization using binary search. The key insight is understanding how ceil(pile / k) affects total time and narrowing the search space smartly. Always consider edge cases — especially empty input, minimum and maximum bounds — when crafting such a solution.

Algorithm Steps

  1. Initialize low = 1, high = max(piles), and answer = max(piles).
  2. While low ≤ high:
  3. → Compute mid = (low + high) / 2 as current eating speed.
  4. → For each pile, calculate hours = ceil(pile / mid).
  5. → Sum total hours for all piles.
  6. → If total hours ≤ h: update answer = mid, and search left by setting high = mid - 1.
  7. → Else, search right with low = mid + 1.
  8. Return answer as the minimum valid eating speed.

Code

C
C++
Python
Java
JS
Go
Rust
Kotlin
Swift
TS
#include <stdio.h>
#include <math.h>

int max(int arr[], int n) {
  int m = arr[0];
  for (int i = 1; i < n; i++)
    if (arr[i] > m) m = arr[i];
  return m;
}

int minEatingSpeed(int piles[], int n, int h) {
  int low = 1, high = max(piles, n), answer = high;
  while (low <= high) {
    int mid = low + (high - low) / 2;
    int hours = 0;
    for (int i = 0; i < n; i++)
      hours += (piles[i] + mid - 1) / mid;

    if (hours <= h) {
      answer = mid;
      high = mid - 1;
    } else {
      low = mid + 1;
    }
  }
  return answer;
}

int main() {
  int piles[] = {3, 6, 7, 11};
  int h = 8;
  int n = sizeof(piles) / sizeof(piles[0]);
  printf("%d\n", minEatingSpeed(piles, n, h));
  return 0;
}

Time Complexity

CaseTime ComplexityExplanation
Best CaseO(n * log m)Where n = number of piles, m = max(pile). Binary search over m with O(n) check each time.
Average CaseO(n * log m)Performs log(m) iterations of binary search with O(n) check per iteration.
Worst CaseO(n * log m)In worst case, binary search runs full range from 1 to max(pile), checking all piles each time.

Space Complexity

O(1)

Explanation: Only a constant number of variables are used for computation. No additional memory is required.


Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...