#875
Koko Eating Bananas
specialist · 875 · lc medium +31 · verified · 49.8% accepted · 13,524 likes · top 37%
Description
A monkey loves eating bananas. There are n piles of bananas; the ith pile has piles[i] bananas. Guards will return in h hours.
The monkey eats at a constant rate of k bananas per hour. Each hour, it selects one pile and eats up to k bananas from it. If a pile has fewer than k bananas, the monkey finishes the pile but does not eat from another pile that hour.
The monkey wants to eat as slowly as possible while still finishing all bananas before the guards return.
Return the minimum integer k such that all bananas can be eaten within h hours.
Example 1:
Input: piles = [3,6,7,11], h = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], h = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], h = 6
Output: 23
Code
1
2
3