medium +25 pts

Koko Eating Bananas

Find the minimum eating speed so Koko eats all bananas within H hours.

Koko loves to eat bananas. There are n piles of bananas, where the i-th 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 k (an integer). Each hour, she chooses one pile and eats k bananas from that pile. If the pile has fewer than k bananas, she eats all of them and will not eat any more bananas during that hour. Write a function min_eating_speed(piles, H) that returns the minimum integer k such that Koko can eat all the bananas within H hours. You may assume that H is at least the number of piles (so an answer always exists).

Constraints

1 <= len(piles) <= 10^4 1 <= piles[i] <= 10^9 len(piles) <= H <= 10^9 Time complexity: O(n log M) where M is the max pile size.

Example

>>> min_eating_speed([3,6,7,11], 8)
4
>>> min_eating_speed([30,11,23,4,20], 5)
30
>>> min_eating_speed([30,11,23,4,20], 6)
23
25 points ~30 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

The time to eat a pile of size p at speed k is ceil(p / k).
The answer is between 1 and max(piles).
Binary search the speed: check if total hours <= H.
If a speed works, try a smaller speed; otherwise try a larger one.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.