11 - Container With Most Water
📌 문제
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of the line i is at (i, ai) and (i, 0). Find two lines, which, together with the x-axis forms a container, such that the container contains the most water.
📋 코드
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height) - 1
result = 0
while left < right:
limit = min(height[left], height[right])
result = max(result, (right - left) * limit)
if height[left] == limit:
left += 1
else:
right -= 1
return result
💡 한마디
left /right 두 인덱스 중 작은 값을 limit
즉, 넓이를 구하는 높이의 경계값이라고 설정하고 풀었다.