Skip to content

Commit 639f73f

Browse files
authored
Solve: Container with most water
1 parent ffd6b99 commit 639f73f

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+

0 commit comments

Comments
ย (0)