Koko Eating Bananas
Medium
Topics
Koko has piles of bananas (nums) and h hours (passed as target) before the guards return. Each hour she eats up to k bananas from one pile. Return the minimum integer speed k so she finishes all piles within h hours.
Example 1
What is being searched is not a position in the array — it is the answer itself, the eating speed.
Cells are the array values; the small numbers underneath are indices.
Input: piles = [3,6,7,11], h = 8 Output: 4
Example 2
Input: piles = [30,11,23,4,20], h = 5 Output: 30
Constraints
- 1 <= piles.length <= 10^4
- piles.length <= h <= 10^9
- 1 <= piles[i] <= 10^9
Editorial — intuition, approach, complexity and pitfalls
Intuition
"Can Koko finish at speed k?" is monotonic: if a speed works, every larger speed works too. That monotonic yes/no boundary is exactly what binary search finds.
So binary-search the speed in [1, max(piles)] for the smallest feasible value.
Approach
- At speed
k, hours needed =sum(ceil(pile / k)). - Binary-search
k: if the hours are withinh, try slower (smaller k); otherwise faster. - Return the smallest feasible speed.
Walkthrough
piles=[3,6,7,11], h=8
k=4 -> ceil: 1+2+2+3 = 8 <= 8 feasible
k=3 -> 1+2+3+4 = 10 > 8 not feasible
smallest feasible = 4Complexity
- Time — O(n log maxPile): each binary-search step scans the piles once.
- Space — O(1): a few scalars.
Common Pitfalls
- Search the answer space (speeds), not the array itself.
mid = (lo + hi) / 2is the textbook form, and in Java or C++ it is the one that bites: the momentlo + hipasses2^31 - 1it wraps negative and the index flies out of range. This problem’s bounds never get close, so the code above is correct as written, butlo + (hi - lo) / 2is the habit worth keeping — same answer, same cost, no overflow. Python’s integers are arbitrary precision, so it is a non-issue there.
Full Solution — all languages
class Solution:
def minEatingSpeed(self, nums, target):
lo, hi = 1, max(nums)
while lo < hi:
mid = (lo + hi) // 2
hours = sum((x + mid - 1) // mid for x in nums)
if hours <= target:
hi = mid
else:
lo = mid + 1
return loimport java.util.*;
class Solution {
public int minEatingSpeed(int[] nums, int target) {
int lo = 1, hi = 0;
for (int x : nums) hi = Math.max(hi, x);
while (lo < hi) {
int mid = (lo + hi) / 2;
long hours = 0;
for (int x : nums) hours += (x + mid - 1) / mid;
if (hours <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int minEatingSpeed(vector<int>& nums, int target) {
int lo = 1, hi = *max_element(nums.begin(), nums.end());
while (lo < hi) {
int mid = (lo + hi) / 2;
long hours = 0;
for (int x : nums) hours += (x + mid - 1) / mid;
if (hours <= target) hi = mid;
else lo = mid + 1;
}
return lo;
}
};/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/ var minEatingSpeed = function(nums, target) {
let lo = 1, hi = Math.max(...nums);
while (lo < hi) {
const mid = (lo + hi) >> 1;
let hours = 0;
for (const x of nums) hours += Math.ceil(x / mid);
if (hours <= target) hi = mid;
else lo = mid + 1;
}
return lo;
};