Skip to content

Commit 182e596

Browse files
committed
container-with-most-water
1 parent d31ada6 commit 182e596

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
'''
4+
1.๋ฌธ์ œ: ๊ฐ€์žฅ ๋งŽ์€ ์–‘์˜ ๋ฌผ์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” max value return
5+
2.์กฐ๊ฑด
6+
- n: ๋†’์ด๋ฅผ ์˜๋ฏธ, ์ตœ์†Œ = 5, ์ตœ๋Œ€ = 10^5
7+
- ์›์†Œ๊ฐ’ ์ตœ์†Œ = 0, ์ตœ๋Œ€ = 10^4
8+
3.ํ’€์ด
9+
- ๋†’์ด๋Š” height[i], height[j] ์ค‘์— ์ž‘์€ ๊ฐ’, ๊ฐ€๋กœ๋Š” abs(i-j)
10+
- output = ๋†’์ด x ๊ฐ€๋กœ
11+
-> 2์ค‘ loop ๋Š” O(n^2) ๋กœ TLE ๋ฐœ์ƒ.
12+
-> two pointer ๋กœ O(n) ์œผ๋กœ ํ•ด๊ฒฐ!
13+
'''
14+
15+
n = len(height)
16+
maxArea = 0
17+
18+
left = 0
19+
right = n-1
20+
21+
while left < right:
22+
curArea = abs(right-left) * min(height[left], height[right])
23+
maxArea = max(curArea, maxArea)
24+
25+
#height ์ด ๋‚ฎ์€์ชฝ pointer update
26+
if height[left] < height[right]:
27+
left += 1
28+
else:
29+
right -= 1
30+
31+
return maxArea
32+

0 commit comments

Comments
ย (0)