Container With Most Water — Why Move the Shorter Wall?
LeetCode 11: trade width for height, trace each decision, and learn from a duplicate-index bug.
Why would we deliberately decrease the width when trying to maximise the area?
That was the question I needed to answer. Each array value is a vertical wall; choose two walls to hold the most water. The distance between their indices gives the width, and the shorter wall limits the water level. Walls between them do not reduce the capacity. The heights do not need to be sorted.
Step through the example below, then try predicting a move or changing the heights. There is no autoplay; take it at your own pace.
Trade width for a chance at more height
Heights: [1, 8, 6, 2, 5, 4, 8, 3, 7]. Maximum area: 49. Indices are zero-based.
Interactive playback requires JavaScript. Read every comparison below.
All 8 comparisons
| Left / right indices | Width × limiting height | Area / best so far | Next movement |
|---|---|---|---|
| 0 / 8 | 8 × 1 | 8 / 8 | Left wall is shorter. Moving the right wall would reduce width without increasing the limiting height, so move the left pointer. |
| 1 / 8 | 7 × 7 | 49 / 49 | Right wall is shorter. Moving the left wall would reduce width without increasing the limiting height, so move the right pointer. |
| 1 / 7 | 6 × 3 | 18 / 49 | Right wall is shorter. Moving the left wall would reduce width without increasing the limiting height, so move the right pointer. |
| 1 / 6 | 5 × 8 | 40 / 49 | The walls are equal. Either direction is safe: keeping either wall while reducing width cannot beat this area. This code moves the right pointer. |
| 1 / 5 | 4 × 4 | 16 / 49 | Right wall is shorter. Moving the left wall would reduce width without increasing the limiting height, so move the right pointer. |
| 1 / 4 | 3 × 5 | 15 / 49 | Right wall is shorter. Moving the left wall would reduce width without increasing the limiting height, so move the right pointer. |
| 1 / 3 | 2 × 2 | 4 / 49 | Right wall is shorter. Moving the left wall would reduce width without increasing the limiting height, so move the right pointer. |
| 1 / 2 | 1 × 6 | 6 / 49 | Right wall is shorter. Moving the left wall would reduce width without increasing the limiting height, so move the right pointer. |
The pointers meet at index 1; there is no distinct pair left. Return 49. The diagram now shows the best container, at indices 1 and 8.
Why the shorter wall moves
area = (right - left) * min(height[left], height[right])Suppose the left wall is shorter. Moving the right wall inward reduces the width, while the left wall still caps the water level. No narrower pair that keeps this left wall can beat the area we just checked. We can safely discard that left wall. The same argument applies when the right wall is shorter.
Moving the shorter wall is the only move that leaves a possibility of improvement. It does not guarantee a larger next area: I keep the maximum seen throughout. With equal heights, either move is safe; my code moves right.
For this example, indices 1 and 8 give width 7 and limiting height 7: area 49. The pointers discard one endpoint per iteration until they meet. That makes the Python solution O(n) time and O(1) extra space. The rewindable teaching trace stores steps separately; that storage is not part of the submitted algorithm.
My accepted Python solution
This is my accepted algorithm with variable names and formatting cleaned up. It compares the current endpoint heights directly and returns the best area, rather than the final area.
class Solution:
def maxArea(self, height: list[int]) -> int:
i = 0
j = len(height) - 1
max_area = 0
while i < j:
current_area = (j - i) * min(height[i], height[j])
if current_area > max_area:
max_area = current_area
if height[i] < height[j]:
i += 1
else:
j -= 1
return max_areaDebugging: a height is not an index
My first submission passed 55 of 65 tests, then failed on duplicate heights. The failing input was:
height = [6,4,3,1,4,6,99,62,1,2,6]Before — looking up a value globally:
minIndex = height.index(min(height[i], height[j]))At i = 5, height[i] is 6, but height.index(6) returns 0: the first occurrence in the whole list. It does not identify the current endpoint. My original solution returned 60; the corrected solution returns 62.
After — compare the endpoints themselves:
if height[i] < height[j]:
i += 1
else:
j -= 1The lookup also adds a linear search inside the loop, potentially making the approach O(n²). Direct endpoint comparison avoids both the duplicate-index mistake and that extra search. Choose the duplicate-index example in the animation to follow the corrected trace.
How I arrived at the solution
I initially considered comparing adjacent walls and expanding the search. That made me focus on height, but area depends on both the shorter height and the distance between the walls. Starting at opposite ends gave the largest width, which left me questioning why reducing it could ever be the right choice.
The useful step was understanding what a move eliminates: keeping the shorter wall cannot improve the area once the width shrinks. Moving that wall discards only pairs that cannot beat the current one. Repeating this gives a linear scan. I then encountered the duplicate-index bug: finding a height’s first occurrence was not the same as identifying the active pointer. Comparing the endpoint values directly fixed it.
Problem and accepted submission
- Platform
- LeetCode
- Problem number
- 11
- Problem difficulty
- Medium
- Pattern
- Two Pointers — Opposite Direction
- Solution language
- Python 3
Accepted · 65 / 65 test cases · 48 ms · 29.52 MB · 18 November 2026. These are my supplied submission results, not browser-animation timings or a benchmark.
Continue with Two Sum II to compare how sorted order justifies a pointer move, or revisit Reverse String for in-place swapping.