File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ ๏ปฟ๏ปฟ #ํด์
2+ #s๋ start index, e๋ ending index๋ก ํ ๋นํ๋ค.
3+ #area ์ (e-s)*min(height[e], height[s]) ๋ก ๋ฉด์ ์ ๋์ด๋ฅผ ๊ตฌํ๋ค.
4+ #๋ง์ฝ height[s]๊ฐ height[e] ๋ณด๋ค ์๋ค๋ฉด, ํ area๋ณด๋ค ๋ ํฐ ๊ฒฐ๊ด๊ฐ์ ์ํด ๋ณํ๋ฅผ ์ค๋ค.
5+ # (e-s)์์ e๋ฅผ ์ค์ด๋ค๋ฉด ํ์ฐ์ ์ผ๋ก area ๊ฐ์ด ๊ธฐ์กด๋ณด๋ค ์ ์ด์ง๋ค. ๋ฐ๋ผ์ s์ 1์ ๋ํด ์ธ๋ฑ์ค๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ์ด๋์์ผ height[s] ์ ๋ณํ๋ฅผ ์ค๋ค.
6+ #๊ทธ ์ธ์ ์ํฉ์๋ height[e]๋ฅผ ๋ณํ์ํค๊ธฐ ์ํด e์ 1๋ฅผ ๋นผ ์ผ์ชฝ ์ธ๋ฑ์ค๋ก ์ด๋์ํจ๋ค.
7+ #ํด๋น ๋ฃจํ๋ s๊ฐ e๋ณด๋ค ์์๋ ์์ฉ๋๋ค. ๋ง์ฝ s์ ์ฆ๊ฐ์ e์ ๊ฐ์๋ก ๋ ๋ณ์๊ฐ ๋ง์ฃผ์น๋ฉด ์ข
๋ฃํ ํ max_area๋ฅผ return์ํจ๋ค.
8+
9+
10+
11+ #Big O
12+ #- N: height์ element ๊ฐฏ์
13+
14+ #Time Complexity: O(N)
15+ #- while : s์ e๊ฐ ๋ง๋ ๋๊น์ง ์ต๋ N๋ฒ ๋ฐ๋ณต๋๋ค. ๊ฐ ๋ฐ๋ณต์์์ ์ฐ์ฐ๋ค์ O(1)์ ํด๋น๋๋ค. -> O(N)
16+
17+
18+ #Space Complexity: O(1)
19+ #- s,e,max_area: ๋ณ์๋ ์์๋ก ์์ฉ๋๋ค -> O(1)
20+ ####
21+ #
22+ #
23+ class Solution (object ):
24+ def maxArea (self , height ):
25+ """
26+ :type height: List[int]
27+ :rtype: int
28+ """
29+ max_area = 0 #Saving for answer
30+ s ,e = 0 ,len (height )- 1 #Assign the first index and last index
31+ while s < e :
32+ area = (e - s ) * min (height [s ],height [e ]) #Current area using e,s
33+ max_area = max (area , max_area ) #Re-assing the max_area comparing with area
34+ if height [s ]< height [e ]:
35+ s += 1
36+ else :
37+ e -= 1
38+ return max_area
39+
40+
You canโt perform that action at this time.
0 commit comments