less than 1 minute read

875. Koko Eating Bananas (medium)

Binary Search Approach

    def minEatingSpeed(self, piles: List[int], H: int) -> int:
        def cannot_eat(piles, H, K):
            hours = 0
            for x in piles:
                if x % K == 0:
                    hours += x // K
                else:
                    hours += x // K + 1
            return hours > H
        
        low, high = 1, max(piles)
        while low < high:
            mid = low + (high - low) // 2
            if cannot_eat(piles, H, mid):
                low = mid + 1
            else:
                high = mid
        return low