Koko Eating Bananas
Koko loves to eat bananas. There are n piles of bananas, the ith pile has piles[i] bananas. The guards have gone and will come back in h hours.
Koko can decide her bananas-per-hour eating speed of k. Each hour, she chooses a pile and eats k bananas from it. If the pile has less than k bananas, she eats all of them and won't eat any more during that hour.
Return the minimum integer k such that she can eat all the bananas within h hours.
Examples
piles = [3,6,7,11], h = 84piles = [30,11,23,4,20], h = 530piles = [30,11,23,4,20], h = 623Constraints
1 <= piles.length <= 10^4piles.length <= h <= 10^91 <= piles[i] <= 10^9
š” Hints
Hint 1
Binary search on the answer: the eating speed k.
Hint 2
The search range is [1, max(piles)].
Hint 3
For each candidate k, calculate total hours needed. If hours <= h, try smaller k.
Algorithm Complexity
Official asymptotic Big-O runtime and auxiliary memory analysis
Binary search on the eating speed. For each candidate speed, calculate total hours. If feasible (<= h), try smaller. Otherwise, try larger.
Binary search over the speed range [1, max(piles)] takes O(log(max(piles))) iterations, each running an O(n) feasibility check.
Feasibility check and binary search use constant O(1) auxiliary variables.
š Real-World Use Cases
Finding the minimum server throughput (processing speed) needed to complete all tasks within a deadline ā binary search on the answer.
š Dry Run
Input: piles=[3,6,7,11], h=8. Binary search speed in [1..11].