Container With Most Water

Key Idea

Solution

class Solution:
    def maxArea(self, height: List[int]) -> int:
        left_pointer = 0
        right_pointer = len(height) - 1

        max_volume = 0

        while left_pointer < right_pointer:
            # Width is the distance between the two lines
            width = right_pointer - left_pointer

            # Water height is limited by the shorter line
            water_height = min(height[left_pointer], height[right_pointer])

            # Current container area
            volume = width * water_height

            # Update max volume
            if volume > max_volume:
                max_volume = volume

            # Move the shorter side inward
            # because the shorter side is the limiting factor
            if height[left_pointer] < height[right_pointer]:
                left_pointer += 1
            else:
                right_pointer -= 1

        return max_volume

Complexity