Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit aa4e1b6

Browse files
committedMay 4, 2025ยท
feat: add container-with-most-water sol
1 parent 3415706 commit aa4e1b6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
 
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,68 @@
11
"""
22
[๋ฌธ์ œํ’€์ด]
33
# Inputs
4+
- height ๋†’์ด์˜ ์ •์ˆ˜ ๋ฐฐ์—ด
45
56
# Outputs
7+
- ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ์ตœ๋Œ€ ๋ฌผ์˜ ์–‘
68
79
# Constraints
10+
- n == height.length
11+
- 2 <= n <= 10^5
12+
- 0 <= height[i] <= 10^4
813
914
# Ideas
15+
- 1. ์™„ํƒ
16+
๊ฒฐ๊ตญ ๊ธฐ๋‘ฅ 2๊ฐœ๋กœ ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ
17+
ํ•˜์ง€๋งŒ, n์€ 10^5 -> 2์ค‘ for๋ฌธ ๋ถˆ๊ฐ€๋Šฅ
18+
19+
2. DP ?
20+
์ตœ๋Œ€ ๋ฌผ ์–‘์„ ์ฐพ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ ์กฐ๊ฑด
21+
22+
ํŠน์ • ๊ตฌ๊ฐ„๋งˆ๋‹ค ์ตœ๋Œ€ ๋กœ ๋‹ด์„ ์ˆ˜ ์žˆ๋Š” ๋ฌผ์˜ ์–‘์ด ๊ฒน์น˜๋Š” ๊ฑธ ํ™œ์šฉ?
23+
height = 1, 8, 6, 2, 5, 4, 8 3 7
24+
dp[i][j]
25+
26+
dp[0][1] = 1
27+
dp[1][2] = 6
28+
dp[2][3] = 2
29+
dp[3][4] = 2
30+
dp[4][5] = 4
31+
dp[5][6] = 4
32+
dp[6][7] = 3
33+
dp[7][8] = 3
34+
35+
dp[0][2] => 0 ~ 2 ์ค‘ ๋†’์ด ๊ฐ€์žฅ ์ž‘์€ -> 1 * (j - i)
36+
dp[1][3] => 1 ~ 3 ์ค‘ ์ž‘์€ 2
37+
38+
=> ๋จผ๊ฐ€ ์œ„ ์ฒ˜๋Ÿผ ์ตœ์†Œ ๋†’์ด ๊ตฌํ•˜๋ฉด์„œ ์ ์  ๊ฐ€๋กœ๊ธธ์ด ๋Š˜๋ ค๊ฐ€๋Š” ์‹์œผ๋กœ ํ’€๋ฉด ๋  ๊ฒƒ ๊ฐ™์€๋ฐ ์ด๋ฅผ dp๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„ํ•˜๋Š” ๋ฒ•์„ ๋ชจ๋ฅด๊ณ˜์Œ..
39+
40+
ํ•ด์„ค ์ฐธ๊ณ 
41+
3. ํˆฌํฌ์ธํ„ฐ
42+
1043
1144
[ํšŒ๊ณ ]
45+
dp, ์ด๋ถ„ํƒ์ƒ‰ ๋„ ์•„๋‹Œ ๊ฒƒ ๊ฐ™๋‹ค๋ผ๋ฉด, ํˆฌํฌ์ธํ„ฐ ๊ณ ๋ คํ•ด๋ณด๊ธฐ
46+
-> dp ๋กœ ๋‹จ์ •์ง€์œผ๋‹ˆ, ๊ตฌ๊ฐ„์˜ ์ตœ์†Ÿ๊ฐ’์ด๋‚˜ ๋ฌผ ์–‘ ์ค‘ ์–ด๋А ๊ฐ’์„ ์ €์žฅํ•ด์•ผํ•˜๋Š”์ง€, ๊ทธ๋ฆฌ๊ณถ ์ €์žฅํ•œ ๊ฐ’์„ ์–ธ์ œ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ• ๊ฑด์ง€..๊ฐ์ด ์•ˆ์žกํ˜€์„œ ๋ชป ํ’ˆ
1247
1348
"""
1449

1550

51+
class Solution:
52+
def maxArea(self, height: List[int]) -> int:
53+
54+
ret = 0
55+
s, e = 0, len(height) - 1
56+
57+
while s < e:
58+
area = (e - s) * min(height[s], height[e])
59+
ret = max(ret, area)
60+
61+
if height[s] < height[e]:
62+
s += 1
63+
else:
64+
e -= 1
65+
66+
return ret
67+
68+

0 commit comments

Comments
 (0)
Please sign in to comment.