Skip to content

Commit bb9a7dd

Browse files
committed
container with most water solution
1 parent 223a5c8 commit bb9a7dd

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
# Naive - ์™„์ „ํƒ์ƒ‰
4+
n = len(height)
5+
6+
"""
7+
๋ชจ๋“  ์กฐํ•ฉ์„ ๊ณ„์‚ฐํ•œ ํ›„ ์ตœ๋Œ“๊ฐ’ ๋ฆฌํ„ด
8+
Tc : O(n^2)
9+
Sc : O(1)
10+
11+
ret = 0
12+
13+
for i in range(n-1):
14+
for j in range(i+1, n):
15+
w = j - i
16+
h = min(height[i], height[j])
17+
ret = max(ret, w*h)
18+
return ret
19+
"""
20+
21+
# Better Solution
22+
"""
23+
ํˆฌ ํฌ์ธํ„ฐ๋ฅผ ํ™œ์šฉํ•œ ๋ฐฉ๋ฒ•
24+
ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ผ ๋•Œ๋Š” ๋” ์ž‘์€ ๊ฐ’์„ ๊ฐ€์ง„ ์ชฝ์ด ํ•œ์นธ ์›€์ง์—ฌ ๊ทธ ๋†’์ด๋ฅผ ๋†’์ด๋Š” ๋ฐฉํ–ฅ ์ชฝ์œผ๋กœ ์ง„ํ–‰
25+
26+
Tc : O(n)
27+
Sc : O(1)
28+
29+
"""
30+
left, right = 0, n-1
31+
ret = 0
32+
33+
while left < right:
34+
w = right - left
35+
h = min(height[left], height[right])
36+
ret = max(ret, h*w)
37+
38+
if height[left] <= height[right]:
39+
left += 1
40+
else:
41+
right -= 1
42+
return ret
43+
44+

0 commit comments

Comments
ย (0)