Here is the implementation:
function minEatingSpeed(piles, h):
left = 1
right = max(piles)
while left < right:
mid = (left + right) / 2
hours = hoursNeeded(piles, mid)
if hours <= h:
right = mid
else:
left = mid + 1
return left
function hoursNeeded(piles, speed):
total = 0
for pile in piles:
total += ceil(pile / speed)
return total
Binary search is where . Each feasibility check is . Total: time.