11. Container With Most Water

Use two pointers strategy, starting with left most and right most, notice that the height is the minimum of these two pillars and then times the width which is rp - lp then get the area. Compare the area with the maximum value, then keep repeating that. If the height of the left pillar is less than the right one, we move left pointer to the right by 1 because we want to find the bigger or taller pillar, and same as the other side

class Solution:
    def maxArea(self, height: List[int]) -> int:
        max_res = 0
        n = len(height)
        lp = 0
        rp = n - 1

        while lp < rp:
            h = min(height[lp], height[rp])
            a = h * (rp-lp)
            max_res = max(max_res, a)

            if height[lp] < height[rp]:
                lp += 1
            else:
                rp -= 1
        return max_res

Last updated