Skip to content

Commit e0cd3f2

Browse files
committed
feat: add container with most water solution
1 parent 17504f2 commit e0cd3f2

File tree

1 file changed

+24
-0
lines changed

1 file changed

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

0 commit comments

Comments
ย (0)