875. Koko Eating Bananas
Python
- Min speed between 1~max(slices)
class Solution:
def minEatingSpeed(self, piles: List[int], h: int) -> int:
min_speed, max_speed = 1, max(piles)
while min_speed <= max_speed:
speed = min_speed + (max_speed - min_speed) // 2
time = self._eating_speed(piles, speed)
if time <= h:
# Too fast
max_speed = speed - 1
else:
# Too slow
min_speed = speed + 1
return min_speed
@staticmethod
def _eating_speed(piles, speed):
time = 0
for pile in piles:
time += math.ceil(pile / speed)
return time