반응형
Notice
Recent Posts
Recent Comments
- Today
- Total
작심삼일
[LeetCode] 11 | Container With Most Water | Python 본문
728x90
반응형
문제 링크: https://leetcode.com/problems/container-with-most-water/
문제
You are given an integer array height of length n. There are n vertical lines drawn such that the two endpoints of the ith line are (i, 0) and (i, height[i]).
Find two lines that together with the x-axis form a container, such that the container contains the most water.
Return the maximum amount of water a container can store.
Notice that you may not slant the container.
조건
- n == height.length
- 2 <= n <= $10^5$
- 0 <= height[i] <= $10^4$
내 풀이
이렇게 연속적인 구간의 최댓값을 구하는 문제는 보통 포인터 2개를 사용해서 해결하면 된다.
코드
class Solution:
def maxArea(self, height: List[int]) -> int:
N = len(height)
start, end = 0, N-1
maxi = 0
while start < end:
maxi = max(maxi, min(height[start], height[end]) * (end-start))
if height[start] < height[end]:
start += 1
else:
end -= 1
return maxi
728x90
반응형
'스터디 > 코테' 카테고리의 다른 글
[LeetCode] 1046 | Last Stone Weight | Python (0) | 2022.04.07 |
---|---|
[LeetCode] 923 | 3Sum With Multiplicity | Python (0) | 2022.04.06 |
[LeetCode] 1721 | Swapping Nodes in an Linked List | Python (0) | 2022.04.04 |
[LeetCode] 344 | Reverse String | Python (0) | 2022.04.01 |
[LeetCode] 410 | Split Array Largest Sum | Python (0) | 2022.03.31 |
Comments