Two PointersMedium

Container With Most Water

Each number is the height of a wall, one step apart. Pick two walls: they hold water up to the shorter one, across the distance between them. Return the most water any pair can hold.

Example 1heights = [4, 7, 3, 6, 5] → 16The first and last walls are 4 apart: min(4, 5) × 4 = 16.
Example 2heights = [5, 5] → 5
2 ≤ n ≤ 10⁵ · 0 ≤ heights[i] ≤ 10⁴

Hints

0 of 3 open
Hint 2 · the patternHint 3 · the key line