File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def maxArea (self , height : List [int ]) -> int :
3
+ # Naive - ์์ ํ์
4
+ n = len (height )
5
+
6
+ """
7
+ ๋ชจ๋ ์กฐํฉ์ ๊ณ์ฐํ ํ ์ต๋๊ฐ ๋ฆฌํด
8
+ Tc : O(n^2)
9
+ Sc : O(1)
10
+
11
+ ret = 0
12
+
13
+ for i in range(n-1):
14
+ for j in range(i+1, n):
15
+ w = j - i
16
+ h = min(height[i], height[j])
17
+ ret = max(ret, w*h)
18
+ return ret
19
+ """
20
+
21
+ # Better Solution
22
+ """
23
+ ํฌ ํฌ์ธํฐ๋ฅผ ํ์ฉํ ๋ฐฉ๋ฒ
24
+ ํฌ์ธํฐ๋ฅผ ์์ง์ผ ๋๋ ๋ ์์ ๊ฐ์ ๊ฐ์ง ์ชฝ์ด ํ์นธ ์์ง์ฌ ๊ทธ ๋์ด๋ฅผ ๋์ด๋ ๋ฐฉํฅ ์ชฝ์ผ๋ก ์งํ
25
+
26
+ Tc : O(n)
27
+ Sc : O(1)
28
+
29
+ """
30
+ left , right = 0 , n - 1
31
+ ret = 0
32
+
33
+ while left < right :
34
+ w = right - left
35
+ h = min (height [left ], height [right ])
36
+ ret = max (ret , h * w )
37
+
38
+ if height [left ] <= height [right ]:
39
+ left += 1
40
+ else :
41
+ right -= 1
42
+ return ret
43
+
44
+
You canโt perform that action at this time.
0 commit comments