[Python] 리트코드 11. Container With Most Water (투 포인터)


[Python] 리트코드 11. Container With Most Water (투 포인터)

가장 많은 물을 담을 수 있도록하는 verical line 2개를 찾는 문제 예전에 코딩테스트에서 비슷한 문제가 나온 적이 있었다. 잘 기억해두자! (그게 투포인터 문제였다니..ㅎ) class Solution: def maxArea(self, height: List[int]) -> int: left = 0 # 순방향 시작점 right = len(height)-1 # 역방향 시작점 maxArea = 0 # 초기 넓이값 while left < right: area = min(height[left], height[right]) * (right-left) # 넓이 구하기 maxArea = max(area, maxArea) # 초기 넓이와 비교해서 update # 범위 좁혀가기 if height[left] < height[right]: # 왼쪽 height가 낮으면 left+=1 # left +1 else: # 오른쪽 height가 낮으면 right-=1 # right-1 return max...



원문링크 : [Python] 리트코드 11. Container With Most Water (투 포인터)