← All posts
November 20, 2026 · 3 min read

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.

Five walls of heights 3, 8, 6, 2, 7 on a ground line; water between the 6 and the 7 rises to the top of the 6, and the readout says 6 × 2 = 12

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:

  1. measure the water: min(height[i], height[j]) * (j - i);
  2. keep it if it beats the best so far;
  3. move the shorter wall one step inward.

Pointer i on the 3 and j on the 7: water four walls wide rises to height 3, the readout says 3 × 4 = 12 and best = 12, with best = max(best, h * (j - i)) highlighted in the Python

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

i has moved to the 8 and the 3 is dimmed; water between the 8 and the 7 rises to 7, the readout says 7 × 3 = 21 and best = 21

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.

i and j meet on the 8 with every other wall dimmed; the readout says returns 21 and return best is highlighted

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.

The best container again: water between the 8 and the 7 capped at the 7, under the readout one pass: O(n)

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?