Koko Eating Bananas — Solution in Python, Java & C++
Koko Eating Bananas solution explained: step-by-step approach, complexity and code in Python, Java, C++ and JavaScript. A medium problem.
Koko Eating Bananas is a medium coding problem on arrays and binary search, asked in Atlassian and Arcesium interviews.
- Difficulty: Medium
- Topics: Arrays, Binary Search
- Asked at: Atlassian, Arcesium
- Time limit: 2 s
- Memory limit: 256 MB
- Languages: JavaScript, TypeScript, Python, Java, C++, C, C#, Go, Kotlin, Swift, Rust, PHP and Ruby
Problem statement
Koko has piles[i] bananas in the i-th pile and h hours before the guards return. Each hour she picks one pile and eats up to k bananas from it (a smaller pile still uses the whole hour).
Return the minimum integer speed k that lets her finish all bananas within h hours.
Example 1
Input: piles = [3,6,7,11], h = 8
Output: 4
Example 2
Input: piles = [30,11,23,4,20], h = 5
Output: 30
Example 3
Input: piles = [30,11,23,4,20], h = 6
Output: 23
Constraints
1 <= piles.length <= 20piles.length <= h <= 2001 <= piles[i] <= 1000
Hints for Koko Eating Bananas
Hint 1
For a fixed k, hours needed = Σ ceil(piles[i]/k) — monotonically decreasing in k.
Hint 2
Binary search the smallest k whose total hours <= h.
How to solve Koko Eating Bananas
Binary search the answer, not the array. The hours Koko needs at speed k decreases monotonically as k grows, so the set of workable speeds is an upward-closed range: once a speed fits in h hours, every faster speed does too. That boundary is exactly what binary search finds.
Approach
- The answer lies in
[1, max(piles)]— no speed above the biggest pile helps, since each hour is capped at one pile. - For a candidate
k, compute the hours needed:sum of ceil(piles[i] / k). - If that total is at most
h,kworks — record it and try slower speeds (hi = mid). - Otherwise
kis too slow — try faster (lo = mid + 1). - The range converges on the smallest workable speed.
Why it works
The feasibility predicate is monotone: raising k can only reduce each ceil(piles[i] / k) term, so total hours never increase. That gives a single true/false boundary over the candidate range, which is precisely the structure binary search needs. The upper bound of max(piles) is safe because at that speed every pile takes exactly one hour, the fewest possible, and h is guaranteed to be at least the number of piles.
Complexity
- Time —
O(n log(max pile)) - Space —
O(1)
Pitfalls
- Hours per pile is a ceiling — a leftover partial pile still costs a whole hour. Integer division alone under-counts; use
(p + k - 1) / k. - Start the search at
1, never0, or the division blows up. - The feasibility test is
hours <= h, so keepmidin range withhi = midrather thanmid - 1. - Summing hours can grow large on big inputs — use a wide enough integer type.
Reference solution
Python
from typing import List
import math
def minEatingSpeed(piles: List[int], h: int) -> int:
lo, hi = 1, max(piles)
while lo < hi:
mid = (lo + hi) // 2
hours = sum(math.ceil(p / mid) for p in piles)
if hours <= h:
hi = mid
else:
lo = mid + 1
return loJava
public static int minEatingSpeed(int[] piles, int h) {
int lo = 1, hi = 1;
for (int p : piles) if (p > hi) hi = p;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
long hours = 0;
for (int p : piles) hours += (p + mid - 1) / mid;
if (hours <= h) hi = mid;
else lo = mid + 1;
}
return lo;
}C++
int minEatingSpeed(vector<int>& piles, int h) {
int lo = 1, hi = 1;
for (int p : piles) if (p > hi) hi = p;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
long long hours = 0;
for (int p : piles) hours += (p + mid - 1) / mid;
if (hours <= h) hi = mid;
else lo = mid + 1;
}
return lo;
}JavaScript
var minEatingSpeed = function(piles, h) {
let lo = 1, hi = Math.max.apply(null, piles);
while (lo < hi) {
const mid = (lo + hi) >> 1;
let hours = 0;
for (const p of piles) hours += Math.ceil(p / mid);
if (hours <= h) hi = mid;
else lo = mid + 1;
}
return lo;
};Also on the editorial tab: C, C#, Go, Kotlin, PHP, Ruby, Rust, Swift, TypeScript.
All 1199 arrays problems · the whole catalogue
Learn the technique: Arrays · Binary Search