Problem intuition
- Brute force tries every pair and computes the area directly.
- The optimal move is to start from the widest container and shrink only the shorter wall, because moving the taller wall cannot increase the height bottleneck.
Pick two lines that trap the maximum water, where area depends on the shorter height and the distance between them.
Two PointersThe solutions below are ordered from least optimal to most optimal, so you can see the improvement path instead of only the final answer.
Solution 1
Solution 2