CodeSpeek

Koko Eating Bananas

Medium · Binary Search

Koko has piles of bananas and a fixed number of hours before the zoo guards return. Each hour she picks exactly one pile and eats up to a chosen speed k bananas from it (if the pile has fewer than k bananas left, she finishes that pile and stops for the hour). Given the pile sizes and the number of hours available, find the smallest integer eating speed k that lets her finish every pile within the given hours.

Examples

Input:  piles = [3,6,7,11], h = 8
Output: 4
Why:    At speed 4 the hours needed are 1+2+2+3=8, which fits exactly; speed 3 needs 9 hours which is too slow.
Input:  piles = [30,11,23,4,20], h = 5
Output: 30
Why:    With only 5 hours and 5 piles, she must clear each pile in a single hour, so speed must cover the largest pile.
Input:  piles = [30,11,23,4,20], h = 6
Output: 23
Why:    At speed 23 the hours needed are 2+1+1+1+1=6, which fits; speed 22 needs 7 hours.

Constraints

1 <= piles.length <= 10^4, piles.length <= h <= 10^9, 1 <= piles[i] <= 10^9

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Koko Eating Bananas

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Koko Eating Bananas. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.