Container With Most Water

mediumTests passed 2026-11-11v1.0.0

1. Core Invariant / The Trick

Start with widest container `(l=0, r=n-1)`. Always move the SHORTER wall inward: advancing the taller wall can only shrink width while keeping height bounded by the untouched shorter wall.

2. Complexity Bounds & Proof

Greedy exchange proof: no pair using the shorter wall with a narrower width can beat the current width.

3. Minimal Idiomatic Code

def maxArea(height: list[int]) -> int:
    l, r = 0, len(height) - 1
    best = 0
    while l < r:
        best = max(best, (r - l) * min(height[l], height[r]))
        if height[l] < height[r]: l += 1
        else: r -= 1
    return best

4. Failure Modes & Interview Follow-ups

Pitfalls:- Confusing with Trapping Rain Water (this looks at pair extremes only, no bar obstruction subtraction).
Follow-ups:- Trapping Rain Water calculates cumulative depth across every index.

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.