0
0
DSA Pythonprogramming

Container With Most Water in DSA Python

Choose your learning style9 modes available
Mental Model
We want to find two lines that hold the most water between them by checking pairs from the edges inward.
Analogy: Imagine two people standing on opposite ends of a riverbank holding a rope. The water they can hold is limited by the shorter person and the distance between them. Moving the shorter person inward might find a better spot to hold more water.
height array: [1] [8] [6] [2] [5] [4] [8] [3] [7]
indexes:      0    1    2    3    4    5    6    7    8
pointers: ↑left                             right↑
Dry Run Walkthrough
Input: height = [1,8,6,2,5,4,8,3,7]
Goal: Find two lines that together with the x-axis form a container holding the most water.
Step 1: Calculate area between left=0 and right=8
left=0 (height=1), right=8 (height=7), area = min(1,7) * (8-0) = 1*8=8
Why: Start with widest container to get a baseline area.
Step 2: Move left pointer from 0 to 1 because height[left] < height[right]
left=1 (height=8), right=8 (height=7), area = min(8,7) * (8-1) = 7*7=49
Why: Moving the shorter line inward might find a taller line and larger area.
Step 3: Move right pointer from 8 to 7 because height[right] < height[left]
left=1 (height=8), right=7 (height=3), area = min(8,3) * (7-1) = 3*6=18
Why: Right line is shorter, so move it inward to try for taller line.
Step 4: Move right pointer from 7 to 6 because height[right] < height[left]
left=1 (height=8), right=6 (height=8), area = min(8,8) * (6-1) = 8*5=40
Why: Right line is still shorter or equal, move inward to find better area.
Step 5: Move left pointer from 1 to 2 because height[left] <= height[right]
left=2 (height=6), right=6 (height=8), area = min(6,8) * (6-2) = 6*4=24
Why: Left line is shorter, move it inward to try for taller line.
Step 6: Move left pointer from 2 to 3 because height[left] < height[right]
left=3 (height=2), right=6 (height=8), area = min(2,8) * (6-3) = 2*3=6
Why: Left line is shorter, move it inward.
Step 7: Move left pointer from 3 to 4 because height[left] < height[right]
left=4 (height=5), right=6 (height=8), area = min(5,8) * (6-4) = 5*2=10
Why: Left line is shorter, move it inward.
Step 8: Move left pointer from 4 to 5 because height[left] < height[right]
left=5 (height=4), right=6 (height=8), area = min(4,8) * (6-5) = 4*1=4
Why: Left line is shorter, move it inward.
Step 9: Move left pointer from 5 to 6, pointers meet, stop
left=6, right=6, pointers meet, end
Why: Pointers crossed, all pairs checked.
Result:
Maximum area found is 49 between lines at index 1 and 8.
Annotated Code
DSA Python
from typing import List

class Solution:
    def maxArea(self, height: List[int]) -> int:
        left, right = 0, len(height) - 1
        max_area = 0
        while left < right:
            width = right - left
            current_area = min(height[left], height[right]) * width
            if current_area > max_area:
                max_area = current_area
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        return max_area

if __name__ == '__main__':
    height = [1,8,6,2,5,4,8,3,7]
    sol = Solution()
    print(sol.maxArea(height))
while left < right:
loop until pointers meet to check all pairs
current_area = min(height[left], height[right]) * (right - left)
calculate area formed by current pair of lines
if current_area > max_area: max_area = current_area
update max area if current is larger
if height[left] < height[right]: left += 1 else: right -= 1
move pointer at shorter line inward to try for taller line
OutputSuccess
49
Complexity Analysis
Time: O(n) because we move two pointers inward only once through the list
Space: O(1) because we use only a few variables, no extra data structures
vs Alternative: Naive approach checks all pairs with O(n^2) time, this two-pointer method is much faster.
Edge Cases
height array with only two lines
Returns area between the two lines directly
DSA Python
while left < right:
height array with all lines of same height
Returns max area using widest distance between lines
DSA Python
if current_area > max_area:
height array with increasing or decreasing heights
Algorithm still finds max area by moving pointers correctly
DSA Python
if height[left] < height[right]:
When to Use This Pattern
When you see a problem asking for max area between two lines or max container, use two pointers from edges moving inward to find the optimal pair efficiently.
Common Mistakes
Mistake: Moving both pointers inward at the same time
Fix: Move only the pointer at the shorter line inward to potentially find a taller line and larger area.
Mistake: Calculating area using max height instead of min height
Fix: Use the minimum height of the two lines to calculate the container area.
Summary
Finds the maximum water container area between two lines using two pointers.
Use when you need to find max area formed by vertical lines and x-axis efficiently.
The key insight is to move the pointer at the shorter line inward to find a potentially taller line and larger area.