Back to LeetCode Series

LeetCode #11: Container With Most Water

Find the two lines that together with the x-axis form a container that holds the most water — using the two-pointer technique.

Difficulty: MediumPattern: Two Pointers, GreedyTime: 7 min read

The Problem

Given an integer array height of length n, find two lines that form a container with the x-axis that can hold the most water.

The container's area is: width × min(height[i], height[j]).

Goal: Maximize the area without slanting the container.

Examples

height: [1, 8, 6, 2, 5, 4, 8, 3, 7] → Max Area: 49  (Between index 1 (8) and 8 (7) → width=7, height=7 → area=49)

height: [1, 1] → Max Area: 1  (Only two lines → area = 1×1 = 1)

height: [4, 3, 2, 1, 4] → Max Area: 16  (First and last (4 and 4) → width=4, height=4 → area=16)

height: [1, 2, 1] → Max Area: 2  (First and last → width=2, height=1 → area=2)

🧠 Key Insight: Why Two Pointers?

The brute force O(n²) solution checks every pair — but we can do better.

Greedy Strategy

  1. Start with pointers at both ends (max width)
  2. Calculate current area
  3. Move the shorter pointer inward
  4. Why? The shorter line limits the height. Moving the taller one can't increase area.
  5. Keep track of the maximum area seen

This reduces time from O(n²) → O(n).

✅ Solution (JavaScript)

function maxArea(height) {
  let left = 0;
  let right = height.length - 1;
  let maxArea = 0;

  while (left < right) {
    const width = right - left;
    const currentHeight = Math.min(height[left], height[right]);
    const area = width * currentHeight;
    
    maxArea = Math.max(maxArea, area);

    // Move the shorter pointer
    if (height[left] < height[right]) {
      left++;
    } else {
      right--;
    }
  }

  return maxArea;
}

🎮 Try It: Water Container Visualizer

Watch how the two pointers move and the area changes in real time.

Water Container Visualizer

Watch the two pointers find the max area

e.g. 1,8,6,2,5,4,8,3,7

800ms
1
8
6
2
5
4
8
3
7
Left: 0 (height: 1)
Right: 8 (height: 7)
Width: 8
Height: 1
Current Area: 8
Max Area So Far: 8
Action: Left (1) < Right (7) → move left →
Step 1 / 9

🌍 Real-World Applications

The "max area" idea appears in geometry, design, and optimization.

📐 Architectural Design

When designing rainwater collectors or solar panels, engineers maximize capture area under height constraints.

📊 Data Visualization

Chart libraries use similar logic to find optimal bin sizes or ranges for histograms.

🧩 Optimization Problems

Logistics, scheduling, and packing problems often use greedy two-pointer strategies to explore trade-offs.

🎮 Game Development

Level design tools might use this to find the largest flat area between obstacles.

💡 Fun Fact: This problem is a simplified version of the "largest rectangle in histogram" problem — which uses a stack and is even more powerful.

Summary

This problem teaches a powerful idea: start wide and greedily shrink based on constraints. The two-pointer technique is used in many array problems — and this visual intuition makes it unforgettable.

View All Challenges