Problem · Array
Koko Eating Bananas
Learn this problemProblem statement
You are given an array piles of positive banana counts and an integer h. Koko chooses one nonempty pile each hour and eats up to k bananas from that pile, where k is a positive integer speed. If the pile contains fewer than k bananas, she empties it and does not start another pile during that hour.
Return the minimum integer speed k that lets Koko empty every pile within at most h hours.
Function
minEatingSpeed(piles: int[], h: int) → intExamples
Example 1
piles = [4,9,13]h = 8return = 4At speed 4, the piles take 1 + 3 + 4 = 8 hours. Speed 3 needs 2 + 3 + 5 = 10 hours, so 4 is minimal.
Example 2
piles = [8,8,8]h = 3return = 8Only one pile can be chosen per hour. With exactly three hours for three piles, each pile must be emptied in one hour, requiring speed 8.
Example 3
piles = [1,7,10,15]h = 10return = 4Speed 4 needs 1 + 2 + 3 + 4 = 10 hours. Speed 3 needs 1 + 3 + 4 + 5 = 13 hours.
Constraints
1 <= piles.length <= 1000001 <= piles[i] <= 1000000000piles.length <= h <= 1000000000