Koko Eating Bananas - Implementation

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 O(logM)O(\log M) where M=max(piles)M = \max(piles). Each feasibility check is O(n)O(n). Total: O(nlogM)O(n \log M) time.