Koko loves to eat bananas. There are n piles of bananas, where the i-th pile has piles[i] bananas. The guards have left and will return in h hours.
Koko can decide her bananas-per-hour eating speed k. Each hour, she chooses some pile of bananas 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.
In this variant, Koko gets tired: after every r consecutive eating hours, she must take one full rest hour during which she eats 0 bananas. The rest hour still counts toward the total h hours.
Return the minimum integer k such that Koko can eat all the bananas within h hours, including any required rest hours. If it is impossible, return -1.
Function Signature
def min_eating_speed(piles: list[int], h: int, r: int) -> int:
Rules
-
k
must be a positive integer.
-
Every hour is either an eating hour, in which she eats from exactly one pile, or a rest hour. Eating hours that follow one another count as consecutive even when they come from different piles.
-
She may never eat for more than
r
hours in a row. A rest hour is only needed between eating hours: once the last banana is eaten she is done, so no rest hour is added after the final eating hour. For example, with
r = 3
and 6 eating hours the schedule is eat, eat, eat, rest, eat, eat, eat, which takes 7 hours.
-
Return
-1
when no positive integer
k
lets her finish within
h
hours.
Constraints
-
1 <= piles.length <= 10^4
-
1 <= piles[i] <= 10^9
-
1 <= h <= 10^9
-
1 <= r <= 10^9
Examples
Example 1
Input: piles = [3,6,7,11], h = 10, r = 3
Output: 4
At k = 4 she needs 1 + 2 + 2 + 3 = 8 eating hours, split into blocks of 3, 3 and 2 with 2 rest hours between them, so 10 hours in total. At k = 3 she needs 10 eating hours and 3 rest hours, 13 in total, which is too many.
Example 2
Input: piles = [3,6,7,11], h = 9, r = 4
Output: 4
At k = 4 the 8 eating hours form two blocks of 4 with one rest hour between them, 9 hours in total, and no rest hour follows the last block. At k = 3 she needs 10 eating hours and 2 rest hours, 12 in total.
Example 3
Input: piles = [5,5,5], h = 3, r = 2
Output: -1
Each pile takes at least one eating hour, and after the first two eating hours she must rest. The shortest possible schedule is eat, eat, rest, eat, which takes 4 hours at any speed and exceeds h = 3.