LC 11 ยท Container With Most Water โ Solution & Explanation | DSA Guide
Two approaches to LeetCode Container With Most Water โ brute force O(nยฒ) and optimal two-pointer O(n). Java and Python solutions with visual walkthrough.
LeetCode ยท #11
Container With Most Water Solution
Given n vertical lines, find two lines that together with the x-axis form a container holding the most water.
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the i-th line are (i, 0) and (i, height[i]). Find two lines that, together with the x-axis, form a container that holds the most water. Return the maximum amount of water.
Example
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output:49// Lines at index 1 (h=8) and index 8 (h=7): min(8,7) ร (8โ1) = 7 ร 7 = 49
Constraints
โข n == height.lengthโข 2 โค n โค 10โตโข 0 โค height[i] โค 10โด
02
Section Two ยท Approach 1
Brute Force โ O(nยฒ)
Try every pair of lines, compute the area for each, and track the maximum. The area formed by lines i and j is min(height[i], height[j]) ร (j โ i).
Problem: O(nยฒ) pairs to check. The key insight is that moving the shorter wall inward is the only way to potentially find a larger area โ moving the taller wall can never help because the area is limited by the shorter wall.
Java โ Brute Force
classSolution {
publicintmaxArea(int[] height) {
int max = 0;
for (int i = 0; i < height.length; i++)
for (int j = i + 1; j < height.length; j++)
max = Math.max(max, Math.min(height[i], height[j]) * (j - i));
return max;
}
}
Python โ Brute Force
classSolution:
defmaxArea(self, height: list[int]) -> int:
max_area = 0for i in range(len(height)):
for j in range(i + 1, len(height)):
max_area = max(max_area, min(height[i], height[j]) * (j - i))
return max_area
Metric
Value
Time
O(nยฒ)
Space
O(1)
03
Section Three ยท Approach 2
Two Pointers โ O(n)
Start with the widest container (pointers at both ends). Compute the area. Then move the pointer pointing to the shorter wall inward โ because keeping the shorter wall and reducing width can only make things worse, but moving the shorter wall might find a taller one.
๐ก Mental model: Imagine two walls of a swimming pool. Water level is limited by the shorter wall. If you slide the shorter wall inward, you lose width but might gain height โ a worthwhile trade. Sliding the taller wall inward loses width AND can't gain height (still limited by the short wall) โ always a loss.
Step 4: Move the pointer with the smaller height inward.
Step 5: Repeat until pointers meet.
๐ฏ Why moving the shorter wall is correct:
The area is min(h_left, h_right) ร width.
If h_left < h_right, then for every position j between left+1 and right, the pair (left, j) has area โค h_left ร (j โ left) < h_left ร width โ always smaller than the current area.
So we can safely discard left and move inward. The greedy choice eliminates all provably-suboptimal pairs.
classSolution {
publicintmaxArea(int[] height) {
int left = 0, right = height.length - 1;
int maxArea = 0;
while (left < right) {
int area = Math.min(height[left], height[right]) * (right - left);
maxArea = Math.max(maxArea, area);
if (height[left] < height[right])
left++; // move shorter wallelse
right--;
}
return maxArea;
}
}
Python โ Two Pointers
classSolution:
defmaxArea(self, height: list[int]) -> int:
left, right = 0, len(height) - 1
max_area = 0while left < right:
area = min(height[left], height[right]) * (right - left)
max_area = max(max_area, area)
if height[left] < height[right]:
left += 1else:
right -= 1return max_area
06
Section Six ยท Analysis
Complexity Analysis
Approach
Time
Space
Trade-off
Brute Force
O(nยฒ)
O(1)
Every pair; too slow for n = 10โต
Two Pointers โ optimal
O(n)
O(1)
Greedy elimination of suboptimal pairs
07
Section Seven ยท Edge Cases
Edge Cases & Pitfalls
Case
Input
Why It Matters
Two elements
[1, 1]
Only one pair. Area = min(1,1) ร 1 = 1.
All same height
[5, 5, 5, 5]
Max area is at widest pair: 5 ร 3 = 15.
Descending
[5, 4, 3, 2, 1]
Right pointer moves inward each time. Max is at start: min(5,1)ร4=4 or min(5,2)ร3=6...
Zero heights
[0, 2, 0]
Any pair with 0 has area 0. Only non-zero pairs matter.
Equal heights
[3, 3]
When heights are equal, either pointer can move โ both discard symmetric candidates.
โ Common Mistake: Moving the taller wall instead of the shorter one. This is always wrong โ moving the tall wall shrinks width while the height stays bottlenecked by the short wall. Only moving the short wall has any chance of increasing area.