Skip to content

Commit f59a4d7

Browse files
committed
container-with-most-water solution
1 parent 3a45e8a commit f59a4d7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def maxArea(self, height: List[int]) -> int:
3+
4+
# ํˆฌํฌ์ธํ„ฐ(two pointer) : ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
5+
left, right = 0, len(height)-1
6+
answer = 0
7+
8+
while left < right:
9+
width = right - left # ์ธ๋ฑ์Šค ์ฐจ์ด๊ฐ€ ๋‘ ๋ฒฝ ์‚ฌ์ด์˜ ๊ฑฐ๋ฆฌ(๋„ˆ๋น„)
10+
h = min(height[left], height[right]) # ๋” ๋‚ฎ์€ ๋ฒฝ์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌผ์„ ์ฑ„์šธ ์ˆ˜ ์žˆ์Œ(๋†’์ด)
11+
12+
answer = max(answer, width * h) # ์ตœ๋Œ€ ๋„“์ด ์—…๋ฐ์ดํŠธ
13+
14+
# ํฌ์ธํ„ฐ ์ด๋™(๋” ๋‚ฎ์€ ๊ฐ’์„ ๊ฐ€์ง„ ํฌ์ธํ„ฐ๋ฅผ ์ด๋™)
15+
if height[left] < height[right]:
16+
left += 1
17+
else:
18+
right -= 1
19+
20+
return answer

0 commit comments

Comments
ย (0)