Sign inEnroll free
DSA Visualizer · 25 visualizationsContainer With Most Water
MediumTwo Pointers

Container With Most Water

You are given an integer array height of length n. Find two lines that, together with the x-axis, form a container that holds the most water. Return the maximum amount of water a container can store.

The step-through player is coming for this one. Two Sum has the full animation today; the rest are being authored in the admin.
Pythontime O(n) · space O(1)
def maxArea(height: list[int]) -> int:
    l, r = 0, len(height) - 1
    best = 0
    while l < r:
        best = max(best, min(height[l], height[r]) * (r - l))
        if height[l] < height[r]: l += 1
        else: r -= 1
    return best
Two Sum is free. Enroll to unlock every visualization and save where you stopped.Enroll free
Watch the video walkthrough