Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created October 4, 2025 22:13
Show Gist options
  • Select an option

  • Save Ifihan/63e9e860e176b8219bab407798ab1586 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/63e9e860e176b8219bab407798ab1586 to your computer and use it in GitHub Desktop.
Container With Most Water

Question

Approach

I use two pointers at the edges and calculate the area formed by the two heights. Since the container height is limited by the shorter line, I move the pointer pointing to the shorter line inward to try and maximize the area. I repeat until the two pointers meet, keeping track of the maximum area found.

Implementation

class Solution:
    def maxArea(self, height: List[int]) -> int:
        left, right = 0, len(height) - 1
        max_area = 0
        
        while left < right:
            width = right - left
            h = min(height[left], height[right])
            max_area = max(max_area, width * h)
            
            if height[left] < height[right]:
                left += 1
            else:
                right -= 1
        
        return max_area

Complexities

  • Time: O(n)
  • Space: O(1)
image
@Iyeoluwaagunbiade
Copy link

Dope

@Ifihan
Copy link
Author

Ifihan commented Dec 21, 2025

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment