728x90
반응형
문제 링크
https://leetcode.com/problems/container-with-most-water/
문제 설명
제한사항
- n == height.length
- 2 <= n <= 105
- 0 <= height[i] <= 104
입출력 예
Example 1:
Input: height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
Output: 49
Explanation: The above vertical lines are represented by array [1, 8, 6, 2, 5, 4, 8, 3, 7]. In this case, the max area of water (blue section) the container can contain is 49.
Example 2:
Input: height = [1, 1]
Output: 1
풀이 과정
정답
- 투 포인터.
class Solution:
def maxArea(self, height: List[int]) -> int:
n = len(height)
left = 0
right = n-1
answer = 0
while left < right:
answer = max(answer, (right-left) * (min(height[left], height[right])))
if height[left] < height[right]:
left += 1
else:
right -= 1
return answer
728x90
반응형
'Problem Solving > 리트코드' 카테고리의 다른 글
[리트코드] 18. 4Sum (0) | 2023.07.11 |
---|