Container With Most Water
Opposite-ends two pointers — always move the shorter wall
Start with the widest possible container and walk the two walls toward each other. Water is capped by the SHORTER wall, so moving the taller one can never help — width shrinks and the cap stays. Moving the shorter wall is the only move that might find something taller, which is why one pass safely covers every candidate pair.
Details
Animated on: Walls of heights [1, 8, 6, 2, 5, 4, 8, 3, 7] — pick the two that trap the most water: area = width × shorter wall.
Problem:Container With Most WaterMedium
Pattern:Two Pointers
Opposite-ends two pointers — always move the shorter wall
step 1 / 12
Walls at both ends: l on height 1, r on height 7. Width 8 is the widest any container will ever be — from here width only shrinks, so every move must gamble on finding a taller wall.
- l=0· arr[0] = 1
- r=8· arr[8] = 7
1FUNCTION maxArea(height):2 set l to 0, r to (length of height) - 1, best to 03 WHILE l < r:4 area = (r - l) * the shorter of height[l] and height[r]5 best = the larger of best and area6 IF height[l] < height[r]: move l one step right7 ELSE: move r one step left8 RETURN best
← / → step · space play · Home restart
Every step in words
Show all 12 steps
- Walls at both ends: l on height 1, r on height 7. Width 8 is the widest any container will ever be — from here width only shrinks, so every move must gamble on finding a taller wall.
- area = (8 - 0) × min(1, 7) = 8 × 1 = 8. The short wall (1) caps the water level — the container leaks over it no matter how tall the other side is. best = 8.
- height[l] = 1 < height[r] = 7, so move l. Keeping wall 1 is pointless: any narrower container it forms is still capped at height 1, so 8 was its best possible area. Discard it.
- area = (8 - 1) × min(8, 7) = 7 × 7 = 49. The gamble paid off — wall 8 raised the cap to 7. New best = 49.
- Now height[r] = 7 < height[l] = 8, so move r. Wall 7 just had its widest, tallest-partner shot and scored 49 — nothing narrower can beat that with it as the cap.
- area = (7 - 1) × min(8, 3) = 6 × 3 = 18. Well short of 49. height[r] = 3 is the shorter wall → r moves again.
- area = (6 - 1) × min(8, 8) = 5 × 8 = 40. Tall walls, but only width 5 — still under 49. Heights are EQUAL, so height[l] < height[r] is false and the else branch moves r.
- area = (5 - 1) × min(8, 4) = 4 × 4 = 16. Width keeps shrinking and the caps aren't rising — 49 holds. height[r] = 4 < 8 → r moves.
- area = (4 - 1) × min(8, 5) = 3 × 5 = 15. To beat 49 at width 3 both walls would need height 17 — impossible here. height[r] = 5 < 8 → r moves.
- area = (3 - 1) × min(8, 2) = 2 × 2 = 4. The tiny wall 2 caps everything. height[r] = 2 < 8 → r moves.
- Last pair: area = (2 - 1) × min(8, 6) = 1 × 6 = 6. height[r] = 6 < 8 → r moves onto l, l < r fails, and the loop ends. Every pair that could have won was checked.
- Answer: walls at indices 1 and 8 (heights 8 and 7) hold the most water — 7 wide × 7 high = 49. Return best = 49, found in one O(n) pass instead of checking all 36 pairs.
Now solve it yourself
Watching is the first half. The second is writing it without the animation.