LeetCode 875 Koko Eating Bananas - Example and Complexity Analysis

Walkthrough and analysis

Trace piles = [3, 6, 7, 11] with h = 8.

Search space: [1, 11].

left = 1, right = 11. mid = 6. Hours = 1 + 1 + 2 + 2 = 6 <= 8. Works. Search left: right = 5.

left = 1, right = 5. mid = 3. Hours = 1 + 2 + 3 + 4 = 10 > 8. Too slow. Search right: left = 4.

left = 4, right = 5. mid = 4. Hours = 1 + 2 + 2 + 3 = 8 <= 8. Works. Search left: right = 3.

left = 4 > right = 3. Done. Minimum speed = 4.

Binary search takes O(logm)O(\log m) iterations where m=max(piles)m = \max(piles). Each iteration computes hours in O(n)O(n). Total: O(nlogm)O(n \log m) time, O(1)O(1) space.