Container With Most Water in Python

The Container With Most Water problem asks us to find two vertical lines that can hold the maximum amount of water. Given an array of heights, we need to find two positions that form a container with the largest area.

Problem Understanding

Given an array of non-negative integers where each value represents the height of a vertical line, we need to find two lines that together with the x-axis form a container that can hold the most water. The area is calculated as width × minimum_height.

Container With Most Water 1 8 6 2 5 4 8 3 7 Area = 7 × 7 = 49

Two-Pointer Approach

The optimal solution uses two pointers starting from both ends. We move the pointer with the smaller height inward, as moving the larger height pointer cannot increase the area.

Algorithm Steps

  • Initialize two pointers: left = 0 and right = length - 1
  • Calculate area using min(height[left], height[right]) × (right - left)
  • Move the pointer with smaller height inward
  • Keep track of maximum area found

Implementation

def max_area(heights):
    left = 0
    right = len(heights) - 1
    max_water = 0
    
    while left < right:
        # Calculate current area
        width = right - left
        current_height = min(heights[left], heights[right])
        current_area = width * current_height
        
        # Update maximum area
        max_water = max(max_water, current_area)
        
        # Move pointer with smaller height
        if heights[left] < heights[right]:
            left += 1
        else:
            right -= 1
    
    return max_water

# Test with example
heights = [1, 8, 6, 2, 5, 4, 8, 3, 7]
result = max_area(heights)
print(f"Maximum water area: {result}")
Maximum water area: 49

Step-by-Step Execution

def max_area_with_steps(heights):
    left = 0
    right = len(heights) - 1
    max_water = 0
    step = 1
    
    print(f"Array: {heights}")
    print(f"{'Step':<4} {'Left':<4} {'Right':<5} {'Heights':<12} {'Width':<5} {'Area':<4} {'Max':<4}")
    print("-" * 50)
    
    while left < right:
        width = right - left
        current_height = min(heights[left], heights[right])
        current_area = width * current_height
        max_water = max(max_water, current_area)
        
        print(f"{step:<4} {left:<4} {right:<5} {heights[left]},{heights[right]:<9} {width:<5} {current_area:<4} {max_water:<4}")
        
        if heights[left] < heights[right]:
            left += 1
        else:
            right -= 1
        step += 1
    
    return max_water

heights = [1, 8, 6, 2, 5, 4, 8, 3, 7]
result = max_area_with_steps(heights)
Array: [1, 8, 6, 2, 5, 4, 8, 3, 7]
Step Left Right Heights     Width Area Max
--------------------------------------------------
1    0    8     1,7         8     8    8   
2    1    8     8,7         7     49   49  
3    1    7     8,3         6     18   49  
4    2    7     6,3         5     15   49  
5    3    7     2,3         4     8    49  
6    4    7     5,3         3     9    49  
7    5    7     4,3         2     6    49  
8    6    7     8,3         1     3    49  

Time and Space Complexity

Metric Two-Pointer Brute Force
Time Complexity O(n) O(n²)
Space Complexity O(1) O(1)

Conclusion

The two-pointer approach efficiently solves the container with most water problem in O(n) time. Always move the pointer with the smaller height, as this gives the best chance to find a larger area in subsequent iterations.

Updated on: 2026-03-25T07:43:22+05:30

513 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements