Container With Most Water (LeetCode 11) in Python
Container With Most Water (LeetCode 11): a pointer at each end, measure the water, keep the best, move the shorter wall in. One pass, O(n).
Container With Most Water (LeetCode 11) gives you a list of wall heights standing in a row and asks how much water the best pair of walls can hold. The walls in between don't block anything: you pick any two, fill the space between them, and measure.
Our example is [3, 8, 6, 2, 7]: five walls at indices 0 to 4. Trying every pair works, but with n walls that is about n²/2 pairs. Two pointers find the answer in one pass.
How much water do two walls hold?
Pour water between two walls and it rises until it reaches the top of the shorter one, then spills over. The taller wall doesn't matter beyond that. So the water is a rectangle: the shorter height times the gap between the two walls, where the gap is the difference of their indices.

Between the 6 at index 2 and the 7 at index 4, the water rises to 6 and spans a gap of 2, so it holds 6 × 2 = 12.
Where do the two pointers start, and which one moves?
Put i on the first wall and j on the last, the widest container there is. Then, while i is left of j:
- measure the water:
min(height[i], height[j]) * (j - i); - keep it if it beats the best so far;
- move the shorter wall one step inward.

Why the shorter one? The shorter wall sets the water line. If you moved the taller wall instead, the gap would shrink by one and the water line could never rise above the shorter wall, which is still there. Every container that keeps the shorter wall and some wall between them holds no more, so the shorter wall can be dropped for good without missing the answer.
How does the walk go on [3, 8, 6, 2, 7]?
| Step | i | j | Walls | Water line | Gap | Water | Best | Move |
|---|---|---|---|---|---|---|---|---|
| 1 | 0 | 4 | 3 and 7 | 3 | 4 | 12 | 12 | i to 1 |
| 2 | 1 | 4 | 8 and 7 | 7 | 3 | 21 | 21 | j to 3 |
| 3 | 1 | 3 | 8 and 2 | 2 | 2 | 4 | 21 | j to 2 |
| 4 | 1 | 2 | 8 and 6 | 6 | 1 | 6 | 21 | j to 1 |

After step 4 the pointers meet on the 8, the loop stops, and the function returns the best it saw: 21, the water between the 8 and the 7.

What does the program look like?
This is the program from the video, line for line:
def max_area(height):
i, j = 0, len(height) - 1
best = 0
while i < j:
h = min(height[i], height[j])
best = max(best, h * (j - i))
if height[i] < height[j]:
i += 1
else:
j -= 1
return best
print(max_area([3, 8, 6, 2, 7]))
Output:
21
When the two walls are the same height, the else branch moves j. That is safe too: with equal walls, neither one can be part of a bigger container with anything between them, since the water line can't rise above that height and the gap only shrinks.
LeetCode's own example, [1, 8, 6, 2, 5, 4, 8, 3, 7], returns 49 with the same function.
Why is it O(n) time and O(1) space?
Every pass of the loop moves one pointer one step toward the other, and they never move back, so there are at most n - 1 passes: O(n) time. The only extra memory is i, j, h and best, whatever the number of walls: O(1) space.

Try it
Keep the same walls but make the last one 10 tall: [3, 8, 6, 2, 10]. Walk the pointers in your head. How much water does the best pair hold now, and which two walls are they?