LeetCode 솔루션 분류
11. Container With Most Water
본문
[Leetcode 시즌 3] 2022년 4월 5일 문제입니다.
[Medium] 11. Container With Most Water
[Medium] 11. 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.
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
Constraints:
n == height.length
2 <= n <= 105
0 <= height[i] <= 104
관련자료
-
링크
댓글 8
JJJJJJJJJJ님의 댓글
- 익명
- 작성일
리트코드 많이 풀어보지 않아서 하루에 한개씩 풀어보려고 ... .
def maxArea(self, height: List[int]) -> int:
max_container, left, right = 0, 0, len(height) -1
while left < right:
cur_container = (right - left) * min (height[left], height[right])
max_container = max (max_container, cur_container)
if height[left] < height[right] :
left += 1
else:
right -= 1
return max_container
def maxArea(self, height: List[int]) -> int:
max_container, left, right = 0, 0, len(height) -1
while left < right:
cur_container = (right - left) * min (height[left], height[right])
max_container = max (max_container, cur_container)
if height[left] < height[right] :
left += 1
else:
right -= 1
return max_container
Stew님의 댓글
- 익명
- 작성일
from collections import deque
class Solution:
def maxArea(self, height: List[int]) -> int:
m, q = 0, deque(height)
while q:
m = max(m, min(q[0], q[-1]) * (len(q) - 1))
if q[0] > q[-1]:
q.pop()
else:
q.popleft()
return m
bobkim님의 댓글
- 익명
- 작성일
class Solution {
public:
int maxArea(vector<int>& height) {
int maxAreaValue=0;
int first=0;
int last=height.size()-1;
while(last>first){
if( (last-first)*(height[first] > height[last] ? height[last] : height[first]) > maxAreaValue){
maxAreaValue=(last-first)*(height[first] > height[last] ? height[last] : height[first]);
};
if(height[first]>height[last]){
last--;
} else {
first++;
};
};
return maxAreaValue;
}
};
핸디맨님의 댓글
- 익명
- 작성일
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)
if area > maxArea:
maxArea = area
else:
if height[left] < height[right]:
left += 1
else:
right -= 1
return maxArea
CANUS님의 댓글
- 익명
- 작성일
class Solution {
func maxArea(_ height: [Int]) -> Int {
var left = 0
var right = height.count-1
var area = 0
while right > left {
let hei = min(height[left], height[right])
let wid = right - left
let newArea = wid * hei
if newArea > area {
area = newArea
}
if height[left] > height[right] {
right -= 1
} else {
left += 1
}
}
return area
}
}
9dea0936님의 댓글
- 익명
- 작성일
class Solution:
def maxArea(self, height: List[int]) -> int:
p1, p2, ans = 0, len(height) - 1, 0
while p1 < p2:
temp = 0
if height[p1] < height[p2]:
temp = (p2 - p1) * height[p1]
p1 += 1
else:
temp = (p2 - p1) * height[p2]
p2 -= 1
ans = max(ans, temp)
return ans