visual walkthrough
Container With Most Water
The classic 'move the shorter side' proof; very frequently asked.
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
| approach | time | space |
|---|---|---|
| Brute force | O(n²) | O(1) |
| Two pointers | O(n) | O(1) |