Container With Most Water in Python

class Solution:
    def maxArea(self, height: List[int]) -> int:
        """
        This function finds the maximum area of water that can be contained by two lines
        from the given list of heights.

        :param height: List[int] - A list of integers where each integer represents the height of a line.
        :return: int - The maximum area of water that can be contained.
        """

        # Initialize left and right pointers
        left, right = 0, len(height) - 1
        # Initialize maxArea to store the maximum area found
        max_area = 0

        # Continue looping until the left pointer is not the same as the right pointer
        while left < right:
            # Calculate the width between the two lines
            width = right - left
            # Find the height of the shorter line between the two
            min_height = min(height[left], height[right])
            # Calculate the area with the current configuration
            current_area = width * min_height
            # Update max_area if the found area is larger
            max_area = max(max_area, current_area)

            # Move the pointer of the shorter line inward
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1

        # Return the maximum area found
        return max_area

PrevNext