LeetEye LeetEye
Easy Two Pointers ~5 min

Container With Most Water

two-pointers container-with-most-water

Problem

You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).

Find two lines that together with the x-axis form a container, such that the container contains the most water.

Return the maximum amount of water a container can store.

Note: You may not slant the container.

Examples

Example 1
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Lines at index 1 (height 8) and index 8 (height 7).
Example 2
Input: height = [1,1]
Output: 1
Key Insight

Start with maximum width (pointers at both ends). Trade width for potentially greater height.

Start wide, greedily sacrifice width by moving the shorter line inward — only way to potentially find more area.

How to Approach This Problem

Pattern Recognition: When you see keywords like container with most water two-pointers, think Two Pointers.

Step-by-Step Reasoning

1 The area of water between lines at positions i and j is:
Answer: min(height[i], height[j]) × (j - i)
Water level = shorter line (water would spill over). Width = distance between lines.
2 Starting with L=0 and R=n-1 gives us:
Answer: The maximum possible width
Pointers at extremes = maximum distance = maximum width. This is our starting point.
3 If height[L] < height[R], which pointer should move?
Answer: Move L (the shorter one) inward
Moving R inward would decrease width while the height is still limited by L (the shorter). Moving L gives a chance of finding a taller line that could compensate for lost width.
4 If height[L]=3 and height[R]=7, moving R to a line of height 5:

- Old area: 3 × width
- New area: min(3, 5) × (width-1) = 3 × (width-1)

What happened?
Answer: Area decreased
The height is still capped at 3 (the short line didn't move). But width decreased. Guaranteed worse or equal, never better.
5 If height[L]=3 and height[R]=7, moving L to a line of height 6:

- Old area: 3 × width
- New area: min(6, 7) × (width-1) = 6 × (width-1)

Under what condition is new area larger?
Answer: When 6 × (width-1) > 3 × width
Simplify: 6w - 6 > 3w → 3w > 6 → w > 2. If width > 2, moving to a taller line helps!
6 Why does always moving the shorter pointer find the global maximum?
Answer: We try all promising pairs; moving taller pointer only finds worse solutions
Moving the taller pointer can never improve (height capped, width decreasing). We're not missing any potentially better solutions by moving the shorter one.

Solution

def maxArea(height: List[int]) -> int:
    left, right = 0, len(height) - 1
    max_water = 0
    
    while left < right:
        # Calculate current area
        width = right - left
        h = min(height[left], height[right])
        max_water = max(max_water, width * h)
        
        # Move the shorter line inward
        if height[left] < height[right]:
            left += 1
        else:
            right -= 1
    
    return max_water

Complexity Analysis

Time O(n)
Space O(1)

Master This Pattern

Build intuition with interactive MCQs. Practice Two Pointers problems in the LeetEye app.

Download LeetEye Free
Practice in LeetEye