← All Patterns

Pattern Recognition Drill

#123

Medium Two Pointers

The Problem

Given n vertical lines, find two that form a container holding the most water.

What approach would you use?

Think about it before scrolling down.

Container With Most Water

Key Signals

Two Pointers

Start at widest. Move the shorter line inward (can't do better keeping it). O(n).

Alt: Greedy

Same logic. Moving the taller line can only decrease width without guaranteeing height increase.

Common Trap

Don't try all O(n²) pairs. The greedy proof: moving the taller side can never improve area.

← #122 #124 →