길이 n의 정수 배열이 제공됩니다.
x축과 함께 용기를 구성하는 두 개의 선을 찾아 용기에 물이 가장 많이 포함되도록 합니다.
용기에 저장할 수 있는 최대 물 양을 반환합니다.
용기를 기울여서는 안 됩니다.
- n == height.length
- 2 <= n <= 105
- 0 <= height[i] <= 104

Input: height = [1,8,6,2,5,4,8,3,7]
Output: 49
Greedy
class Solution:
def maxArea(self, height: List[int]) -> int:
left, right = 0, len(height)-1
area=0
while left < right:
width = right-left
min_h = min(height[left],height[right])
area = max(area,width*min_h)
if height[left]>height[right]:
right-=1
while height[right] < min_h:
right-=1
else:
left+=1
while height[left] < min_h:
left+=1
return area
'코딩테스트' 카테고리의 다른 글
[leetcode] 이진탐색 1011. Capacity To Ship Packages Within D Days (0) | 2023.02.20 |
---|---|
꼭 알아야하는 코딩테스트 기초 알고리즘 (0) | 2023.02.08 |
[백준] 8983번 사냥꾼 (0) | 2023.02.06 |
[leetcode] 13. Roman to Integer : python (0) | 2023.01.19 |
[leetcode] 12. Integer to Roman : python (0) | 2023.01.17 |