[LeetCode/Python] 11. Container With Most Water

도니·2025년 9월 28일

Interview-Prep

목록 보기
22/29
post-thumbnail

📌 Problem

[LeetCode] 11. Container With Most Water

📌 Solution

Idea

  • Place two pointers at both ends and move them inward while updating the maximum area
  • As the shorter line limits the water, we move the pointer on the smaller height:
    - If the left line is shorter, move l += 1
    - If the right line is shorter, move r -= 1

Code

class Solution:
    def maxArea(self, height: List[int]) -> int:
        n = len(height)
        l, r = 0, n-1
        max_area = 0
        
        while l < r:
            area = (r - l) * min(height[l], height[r])
            max_area = max(area, max_area)
            
            if height[l] < height[r]:
                l += 1
            else: 
                r -= 1
        
        return max_area
profile
Where there's a will, there's a way

0개의 댓글