Koko Eating Bananas
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 tokbananas from it; if it has fewer, you finish it and wait for the next hour.- returns
- The smallest whole speed
kthat eats every pile withinhhours. - constraints
- 1 ≤ piles.length ≤ 10⁴
- 1 ≤ piles[i] ≤ 10⁹
- piles.length ≤ h ≤ 10⁹
- 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
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more binary search problems
- 704Binary SearchEasysolve
- 35Search Insert PositionEasysolve
- 69Sqrt(x)Easysolve
- 153Find Minimum in Rotated Sorted ArrayMediumsolve
- 33Search in Rotated Sorted ArrayMediumsolve
- 1011Capacity To Ship Packages Within D DaysMediumsolve
- 274H-IndexMediumsolve
- 74Search a 2D MatrixMediumsolve
- 162Find Peak ElementMediumsolve
- 34Find First and Last Position of Element in Sorted ArrayMediumsolve
- 4Median of Two Sorted ArraysHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.