Koko Eating Bananas

mediumTests passed 2026-11-11v1.0.0

1. Core Invariant / The Trick

Monotonic predicate on speed `k` over `[1 .. max(piles)]`: calculate `sum(ceil(p / k))` (`(p + k - 1) // k` integer ceiling). Find the smallest `k` where total hours <= `h`.

2. Complexity Bounds & Proof

Binary search spans range `1..max(piles)` (size M); each check iterates N piles.

3. Minimal Idiomatic Code

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

4. Failure Modes & Interview Follow-ups

Pitfalls:- Integer division truncation vs ceiling division `(p + k - 1) // k`.
Follow-ups:- Capacity To Ship Packages Within D Days uses identical binary search on answer capacity.

Personal Notes & Scratchpad (Local-First Yjs CRDT)

Saved instantaneously in IndexedDB. Streams live across peers via Hocuspocus when signed in.

Related Invariants

Browser Scratchpad
Console Output
Click Run or press Cmd/Ctrl+Enter to execute locally in a Web Worker.