Back to Problem
Official 176 views

Editorial: Container With Most Water

by admin ·

3

1 Intuition

The shorter wall limits the area, so moving the taller wall inward can never help.

2 Approach

Start with the widest container and always move the pointer at the shorter wall.

3 Complexity Analysis

Time O(n), Space O(1).

4 Solution Code

class Solution:
    def maxArea(self, height: list[int]) -> int:
        lo, hi, best = 0, len(height) - 1, 0
        while lo < hi:
            best = max(best, (hi - lo) * min(height[lo], height[hi]))
            if height[lo] < height[hi]:
                lo += 1
            else:
                hi -= 1
        return best

5 Code Walkthrough

The reference solution below implements the approach step by step.