Skip to content

Commit ad4b1cb

Browse files
committed
implement maxArea function to calculate maximum water container area
1 parent 4c4c415 commit ad4b1cb

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
# Approach
3+
ํˆฌํฌ์ธํ„ฐ๋กœ ์–‘ ๋์—์„œ ๋ฌผ์˜ ์–‘์„ ๊ณ„์‚ฐํ•ด๋‚˜๊ฐ‘๋‹ˆ๋‹ค.
4+
๋” ๋‚ฎ์€ height์ธ ์ชฝ์˜ ํฌ์ธํ„ฐ๋ฅผ ์ค„์—ฌ๋‚˜๊ฐ€๋Š”๋ฐ, ๋†’์ด๊ฐ€ ๊ฐ™์€ ๊ฒฝ์šฐ์—๋Š” ์–‘์ชฝ ๋ชจ๋‘ ํฌ์ธํ„ฐ๋ฅผ ์›€์ง์ž…๋‹ˆ๋‹ค.
5+
6+
# Complexity
7+
height์˜ ๊ธธ์ด๊ฐ€ N์ผ ๋•Œ
8+
- Time complexity: O(N)
9+
- Space complexity: O(1)
10+
"""
11+
12+
13+
class Solution:
14+
def maxArea(self, height: list[int]) -> int:
15+
n = len(height)
16+
left, right = 0, n - 1
17+
best = 0
18+
while left < right:
19+
best = max(best, (right - left) * min(height[left], height[right]))
20+
if height[left] > height[right]:
21+
right -= 1
22+
else:
23+
left += 1
24+
return best

0 commit comments

Comments
ย (0)