Koko Eating Bananas
Problem statement
Koko has n piles of bananas. Pile i holds piles[i] bananas. She has h hours to eat all of them.
Koko picks one whole-number eating speed k (bananas per hour) and keeps it the whole time. Each hour she chooses one pile and eats k bananas from it. If that pile has fewer than k bananas left, she eats all of them and does not start another pile during that hour.
Return the smallest speed k that lets her finish every pile within h hours.
Function
minEatingSpeed(piles: int[], h: int) → intExamples
Example 1
piles = [4,9,7]h = 6return = 4At speed 4, the piles take 1, 3, and 2 hours, which is 6 hours in total. At speed 3, they take 2, 3, and 3 hours, which is 8 hours, too many.
Example 2
piles = [12,5,20,8]h = 4return = 20There are 4 piles and 4 hours, so each pile must be finished in one hour. The biggest pile has 20 bananas, so the speed must be 20.
Example 3
piles = [7]h = 3return = 3At speed 3, Koko eats 3, 3, and then the last 1: 3 hours. At speed 2, she needs 4 hours (2, 2, 2, 1).
Constraints
1 <= piles.length <= 10^4piles.length <= h <= 10^91 <= piles[i] <= 10^9