InterviewHubPRO
Section Overview
Q50

Question 50 of 100

Part 6: Binary Search & Search on Answer (46 - 52)

50%
DSA Patterns (Multi-Lang)•Part 6: Binary Search & Search on Answer (46 - 52)

Koko Eating Bananas.

In Simple Words: Binary Search on Answer range [1, max(piles)]. For eating rate k, calculate hours required: Math.ceil(p / k). If hours <= h, try slower rate r = k; else l = k + 1.

  • Time: $O(N \log(\max(P)))$ | Space: $O(1)$
Multi-Language Solutions
Switch tabs or choose All
import math

def minEatingSpeed(piles: list[int], h: int) -> int:
    l, r = 1, max(piles)
    while l < r:
        k = l + (r - l) // 2
        total_hours = sum(math.ceil(p / k) for p in piles)
        if total_hours <= h:
            r = k
        else:
            l = k + 1
    return l