mediumArrayTwo PointersTwo Pointers

Container With Most Water

mediumTime: O(n)Space: O(1)

Recognize the pattern

maximize area between two linesheights as barswidth vs height tradeoff

Brute force idea

A straightforward first read of Container With Most Water is this: Check every pair of lines. That instinct is useful because it follows the prompt literally, but it usually keeps revisiting work the problem is begging you to organize.

Better approach

The deeper shift in Container With Most Water is this: Two pointers at edges, move the shorter line inward. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Key invariant

At the center of Container With Most Water is one steady idea: Moving the taller line can only decrease area (width shrinks, height can't improve), so always move the shorter one. When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Watch out for

The trap in Container With Most Water usually looks like this: Moving the taller line — you should always move the shorter one to potentially find a taller line. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Two Pointers Pattern