Skip to content

Commit ea0e395

Browse files
committed
container-with-most-water solution (ts, py)
1 parent ff5dad5 commit ea0e395

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""
2+
너비: e - s
3+
높이: min(height[s], height[e])
4+
5+
넓이 = e - s * min(height[s], height[e])
6+
"""
7+
8+
# TC: O(N^2), SC: O(1)
9+
# 모든 경우의 수를 따져 가장 넓이가 크게 나오는 경우를 찾는 풀이 -> Time Limit Exceeded
10+
class Solution:
11+
def maxArea(self, height: List[int]) -> int:
12+
max_area = 0
13+
14+
for s in range(len(height) - 1):
15+
for e in range(s + 1, len(height)):
16+
area = (e - s) * min(height[s], height[e])
17+
max_area = max(area, max_area)
18+
return max_area
19+
20+
21+
# 투 포인터 풀이
22+
# TC: O(N), SC: O(1)
23+
class Solution:
24+
def maxArea(self, height: List[int]) -> int:
25+
max_area = 0
26+
s, e = 0, len(height) - 1
27+
while s < e:
28+
area = (e - s) * min(height[s], height[e])
29+
max_area = max(area, max_area)
30+
if height[s] < height[e]:
31+
s += 1
32+
else:
33+
e -= 1
34+
35+
return max_area
36+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
너비: (e - s)
3+
높이: Math.min(height[s], height[e])
4+
5+
넓이: (e - s) * Math.min(height[s], height[e])
6+
7+
# TC: O(N), SC: O(1)
8+
*/
9+
10+
function maxArea(height: number[]): number {
11+
let maxArea = 0;
12+
let s = 0,
13+
e = height.length - 1;
14+
while (s < e) {
15+
maxArea = Math.max((e - s) * Math.min(height[s], height[e]), maxArea);
16+
if (height[s] > height[e]) {
17+
e -= 1;
18+
} else {
19+
s += 1;
20+
}
21+
}
22+
return maxArea;
23+
}

0 commit comments

Comments
 (0)