Skip to content

visual walkthrough

Container With Most Water

MediumOpposite EndsReported at: AmazonMicrosoftMeta+6

The classic 'move the shorter side' proof; very frequently asked.

Solve on LeetCode

The idea

Start with the widest container. Its height is limited by the shorter wall, so the only way to do better is to find a taller wall, which means moving the shorter side inward.

Moving the taller side can never help: the container gets narrower and is still capped by the same shorter wall.

Complexity

approachtimespace
Brute forceO(n²)O(1)
Two pointersO(n)O(1)

More walkthroughs