Koko Eating Bananas

Medium#875search the answer

There are piles of bananas and h hours. Each hour you pick one pile and eat up to k bananas from it; if the pile has fewer, you finish it and wait for the next hour. Find the smallest whole speed k that eats every pile within h hours. Our example: piles [4, 9, 2, 13], h = 6 — the answer is 7.

input
piles: how many bananas each pile has; h: the hours there are. Each hour you take one pile and eat up to k bananas from it; if it has fewer, you finish it and wait for the next hour.
returns
The smallest whole speed k that eats every pile within h hours.
constraints
  • 1 ≤ piles.length ≤ 10⁴
  • 1 ≤ piles[i] ≤ 10⁹
  • piles.length ≤ h ≤ 10⁹
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[]} piles
 * @param {number} h
 * @return {number}
 */
function minEatingSpeed(piles, h) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more binary search problems

Pattern “Binary search” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.