← Pointer Manipulation

Micro-Drill #88 — Container with most water setup

Pointer Manipulation Target: 10s

Greedy narrowing of left/right pointers maximizes area. Proof relies on the shorter-wall argument.

l, r = 0, len(h) - 1
best = 0
while l < r:
    area = min(h[l], h[r]) * (r - l)
    best = max(best, area)
    if h[l] < h[r]: l += 1
    else: r -= 1

Type it from memory. Go.

Practice Problems

Related Coding Drills

← Micro #87 Micro #89 →