Choose two vertical lines that maximize width × the shorter wall. The no-slant rule makes the limiting height explicit.
Start wide; discard the shorter wall, because width can only shrink and the taller wall cannot help until the shorter side improves.
For endpoints L and R, the container holds (R − L) × min(height[L], height[R]). Begin with maximum width, then throw away only a wall that currently limits the height.
Each frame evaluates one pair, records its area, and advances exactly one pointer. The bars marked as the best container stay highlighted while the active pointers move inward.
class Solution:
def maxArea(self, height: list[int]) -> int:
left, right = 0, len(height) - 1
best = 0
while left < right:
width = right - left
waterline = min(height[left], height[right])
best = max(best, width * waterline)
if height[left] <= height[right]:
left += 1
else:
right -= 1
return best
The area is measured before the move. On a tie, moving either side is safe; this version advances the left pointer.
left += 1 when height[left] ≤ height[right]The left wall is the limiting height, so every pair that keeps it has both smaller width and no taller limiting wall.right -= 1 when height[right] < height[left]Symmetrically, discard the right wall when it is shorter; a better area needs a taller right endpoint.right - leftEvery move narrows the container by at least one index, so the only hope of improvement is discovering a taller short side.The winning endpoints are indices 1 and 8: width 7 and limiting height min(8, 7) = 7.
The equal outer walls survive as the best pair: width 4 × height 4. The tempting middle walls only reduce both width and height.
“Area is width times the shorter endpoint. I start with the widest pair and move only the shorter endpoint, because keeping it while shrinking width cannot help. Each pointer moves once, so the scan is linear and constant-space.”