Skip to content

Container With 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 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. Notice that you may not slant the container.

InputOutput
[1,8,6,2,5,4,8,3,7]49 (between index 1 and 8: min(8,7)*7=49)
[1,1]1
  • n == height.length
  • 2 <= n <= 10^5
  • 0 <= height[i] <= 10^4
ApproachTimeSpaceBest When
Two PointersO(n)O(1)Always — this is the optimal solution
Brute ForceO(n²)O(1)Understanding only
★ Recommended
⏱ Time O(n) 💾 Space O(1)

Start with the widest possible container — left pointer at index 0, right pointer at index n-1. At each step, compute the current area. Then move the pointer pointing to the shorter wall inward; keeping the taller pointer would only decrease width without any chance of a taller container.

Greedy proof: Suppose height[left] < height[right]. Moving right inward:

  • Width decreases by 1
  • New height = min(height[left], height[new_right]) ≤ height[left] — the left wall still caps the height
  • So area can only decrease or stay the same → moving the taller pointer is never optimal
  • Therefore, always move the SHORTER pointer

Input: height = [1,8,6,2,5,4,8,3,7]

steplefth[l]righth[r]widthareamaxmove
10187888left++ (shorter)
2188774949right—
3187361849right—
4186854049left++ (tie)

Animated walkthrough

Two-pointer walk — tracking max area

Use Next or Autoplay to watch the left and right pointers converge, and see how the max area is updated.

Step 1 of 4
Waiting to begin...
Array state
Memory / lookup state

function maxArea(height):
left, right = 0, len(height) - 1
max_water = 0
while left < right:
water = min(height[left], height[right]) * (right - left)
max_water = max(max_water, water)
if height[left] <= height[right]:
left++
else:
right--
return max_water
container_with_most_water_two_pointer.py
"""
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.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Explanation: The vertical lines are drawn at the above positions. The maximum area is
obtained between index 1 and index 8: min(8,7) * (8-1) = 49.
Example 2:
Input: height = [1,1]
Output: 1
"""
# Approach: Two Pointers
# Place one pointer at the start and one at the end. The area is min(height[left], height[right]) * (right - left).
# Always move the pointer with the shorter height inward — the shorter wall is the bottleneck,
# so moving the taller pointer can only decrease or maintain the area.
# Time Complexity: O(n) — single pass
# Space Complexity: O(1)
from typing import List
def max_area(height: List[int]) -> int:
left, right = 0, len(height) - 1
max_water = 0
while left < right:
water = min(height[left], height[right]) * (right - left)
max_water = max(max_water, water)
if height[left] <= height[right]:
left += 1
else:
right -= 1
return max_water
print(max_area([1, 8, 6, 2, 5, 4, 8, 3, 7])) # 49
print(max_area([1, 1])) # 1
✓ Simple
⏱ Time O(n²) 💾 Space O(1)

Check every possible pair of lines using two nested loops. For each pair (i, j), compute area = min(height[i], height[j]) * (j - i) and track the maximum. Simple to understand but too slow for large inputs.

Input: height = [1,8,6,2,5,4,8,3,7]

ijh[i]h[j]widthareamax
0118111
0216222
188774949
function maxAreaBruteForce(height):
n = len(height)
max_water = 0
for i in range(n):
for j in range(i + 1, n):
water = min(height[i], height[j]) * (j - i)
max_water = max(max_water, water)
return max_water
container_with_most_water_brute_force.py
"""
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.
Notice that you may not slant the container.
Example 1:
Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Example 2:
Input: height = [1,1]
Output: 1
"""
# Approach: Brute Force
# Try every pair (i, j). Area = min(height[i], height[j]) * (j - i). Track the maximum.
# Time Complexity: O(n^2)
# Space Complexity: O(1)
from typing import List
def max_area_brute_force(height: List[int]) -> int:
n = len(height)
max_water = 0
for i in range(n):
for j in range(i + 1, n):
water = min(height[i], height[j]) * (j - i)
max_water = max(max_water, water)
return max_water
print(max_area_brute_force([1, 8, 6, 2, 5, 4, 8, 3, 7])) # 49
print(max_area_brute_force([1, 1])) # 1
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
container_with_most_water_optimized.py
"""
Solution for Container With Most Water
"""
def solve():
"""Implementation for approach 3 of Container With Most Water"""
pass
if __name__ == "__main__":
print("Solution ready")