Skip to content

Commit 63f9feb

Browse files
committed
Container With Most Water
1 parent a64d499 commit 63f9feb

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
πŸ“š 11. Container With Most Water
3+
4+
πŸ“Œ 문제 μš”μ•½
5+
- 높이 배열이 μ£Όμ–΄μ‘Œμ„ λ•Œ, 두 λ§‰λŒ€ 사이에 담을 수 μžˆλŠ” 물의 μ΅œλŒ€ 넓이 κ΅¬ν•˜κΈ°
6+
- 넓이 = min(두 높이) Γ— 거리
7+
8+
🎯 핡심 μ•Œκ³ λ¦¬μ¦˜
9+
- νŒ¨ν„΄: 투 포인터 (Two Pointer)
10+
- μ‹œκ°„λ³΅μž‘λ„: O(n)
11+
- κ³΅κ°„λ³΅μž‘λ„: O(1)
12+
13+
πŸ’‘ 핡심 아이디어
14+
1. μ–‘ λμ—μ„œ μ‹œμž‘ (left = 0, right = n-1)
15+
2. ν˜„μž¬ 넓이 계산 ν›„ μ΅œλŒ“κ°’ κ°±μ‹ 
16+
3. 더 μž‘μ€ 높이 μͺ½μ„ 이동 β†’ 더 큰 높이λ₯Ό 찾을 κ°€λŠ₯μ„±!
17+
"""
18+
19+
class Solution:
20+
def maxArea(self, height: list[int]) -> int:
21+
left, right = 0, len(height) - 1
22+
max_area = 0
23+
24+
while left < right:
25+
h = min(height[left], height[right])
26+
w = right - left
27+
max_area = max(max_area, h * w)
28+
29+
if height[left] < height[right]:
30+
left += 1
31+
else:
32+
right -= 1
33+
34+
return max_area

0 commit comments

Comments
Β (0)