LeetCode 875 Koko Eating Bananas - Implementation

The approach

Binary search on speed. For each speed, compute total hours and check if it fits.

function minEatingSpeed(piles, h): left = 1 right = max(piles) result = right while left <= right: mid = (left + right) // 2 hours = sum(ceil(pile / mid) for pile in piles) if hours <= h: result = mid right = mid - 1 else: left = mid + 1 return result

O(nlogm)O(n \log m) time, O(1)O(1) space.