LeetCode 11 · Medium

Container With Most Water Explained (LeetCode 11)

Updated Nov 27, 2026 · 3 min read

Put one pointer at each end. The water between two walls is the shorter height times the distance between them. Measure it, keep the best, then move the pointer at the shorter wall inward, because moving the taller one can only shrink the width without raising the height. Stop when the pointers meet: O(n) time, O(1) space.

Watch onYouTubeInstagram

Key points

  • ●Water between two walls = shorter height × distance.
  • ●Checking every pair is O(n²): 15 pairs for 6 walls, about half a trillion for a million.
  • ●Start at both ends and always move the shorter wall inward.
  • ●Moving the taller wall can never beat the current area, so those pairs are safe to skip.
  • ●Two pointers, one pass: O(n) time, O(1) space.
Step-by-step visual sheet: Container With Most Water
The full visual sheet, made with Edithly.Open the interactive sheet

The riddle: one tarp, six fence posts

Maya has a row of fence posts, one unit apart, with heights 3, 7, 2, 6, 4, 5. She has one tarp and wants to tie it between two posts to catch rain. Which two posts catch the most?

The water level can't go above the shorter post, or it spills. So the rain caught is the shorter height times the gap between the posts. That's LeetCode 11, Container With Most Water: given a list of heights, find the largest amount of water any two of them can hold.

For Maya's posts, the answer is 20: the posts of height 7 and 5, which are 4 apart. The shorter is 5, and 5 × 4 = 20.

The slow way: try every pair

Measure every pair and keep the biggest. Six posts have 15 pairs. A million posts have about half a trillion. That's O(n²), and the problem wants O(n).

The idea: always move the shorter wall

Start with the widest container possible, one pointer at each end. Measure it. Now you have to give up one of the two posts. Which one?

Say the left post is the shorter one. If you move the right post inward instead:

  • the width gets smaller, and
  • the height is still capped by that same short left post (or something even shorter).

So the area can't go up. Every container that keeps the short left post and swaps the right one is no better than what you just measured, and you can skip them all at once. The only move that might help is dropping the short post and hoping the next one is taller.

The rule: measure, save the best, move the shorter side, repeat until the pointers meet.

Trace it on Maya's posts

left, right heights area best move
0, 5 3 and 5 3 × 5 = 15 15 left (3 is shorter)
1, 5 7 and 5 5 × 4 = 20 20 right (5 is shorter)
1, 4 7 and 4 4 × 3 = 12 20 right
1, 3 7 and 6 6 × 2 = 12 20 right
1, 2 7 and 2 2 × 1 = 2 20 right

Five looks instead of 15 pairs, and the best is 20. The winner showed up on the second look, but the scan had to finish to be sure nothing beat it. The visual sheet above builds the area formula in step 1 and proves the "move the shorter wall" rule in step 3.

The code in Python

def max_area(heights):
    left, right = 0, len(heights) - 1
    best = 0
    while left < right:
        width = right - left
        height = min(heights[left], heights[right])
        best = max(best, width * height)
        if heights[left] <= heights[right]:
            left += 1
        else:
            right -= 1
    return best

On a tie the code moves the left pointer. Moving either side is fine there.

Complexity

  • Time: O(n). Each step moves one pointer one place, so there are n - 1 steps.
  • Space: O(1), three variables.

From about n²/2 pairs to n looks, with one rule.

The trap to watch for

Moving the taller wall. It feels natural to keep the tall post since it looks promising, but it's the short post that limits the water. Keep the short one and the area can only shrink.

Forgetting the width. The tallest two posts aren't always the answer. Here 7 and 6 are the two tallest, but they're only 2 apart, which holds 12. The 7 and 5 pair wins because it's wider.

Frequently asked questions

Why do you move the shorter wall in Container With Most Water?

The water can't rise above the shorter wall. If you move the taller wall inward instead, the width shrinks and the height is still capped by the same short wall, so the area can only stay the same or drop. Moving the shorter wall is the only move that might find a taller limit.

What is the time complexity of Container With Most Water?

The two-pointer solution is O(n) time, because each step moves one pointer one place and they meet after n - 1 steps. It uses O(1) extra space. Brute force over every pair is O(n²).

Which pointer do you move when both walls are the same height?

Either one. With equal heights, moving either side can't beat the current area with the other one fixed, so the code just moves left when heights[left] <= heights[right].

Is Container With Most Water the same as Trapping Rain Water?

No. Container With Most Water picks just two walls and ignores everything between them. Trapping Rain Water adds up the water that sits on top of every bar.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free