File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ 너비: e - s
3+ 높이: min(height[s], height[e])
4+
5+ 넓이 = e - s * min(height[s], height[e])
6+ """
7+
8+ # TC: O(N^2), SC: O(1)
9+ # 모든 경우의 수를 따져 가장 넓이가 크게 나오는 경우를 찾는 풀이 -> Time Limit Exceeded
10+ class Solution :
11+ def maxArea (self , height : List [int ]) -> int :
12+ max_area = 0
13+
14+ for s in range (len (height ) - 1 ):
15+ for e in range (s + 1 , len (height )):
16+ area = (e - s ) * min (height [s ], height [e ])
17+ max_area = max (area , max_area )
18+ return max_area
19+
20+
21+ # 투 포인터 풀이
22+ # TC: O(N), SC: O(1)
23+ class Solution :
24+ def maxArea (self , height : List [int ]) -> int :
25+ max_area = 0
26+ s , e = 0 , len (height ) - 1
27+ while s < e :
28+ area = (e - s ) * min (height [s ], height [e ])
29+ max_area = max (area , max_area )
30+ if height [s ] < height [e ]:
31+ s += 1
32+ else :
33+ e -= 1
34+
35+ return max_area
36+
Original file line number Diff line number Diff line change 1+ /**
2+ 너비: (e - s)
3+ 높이: Math.min(height[s], height[e])
4+
5+ 넓이: (e - s) * Math.min(height[s], height[e])
6+
7+ # TC: O(N), SC: O(1)
8+ */
9+
10+ function maxArea ( height : number [ ] ) : number {
11+ let maxArea = 0 ;
12+ let s = 0 ,
13+ e = height . length - 1 ;
14+ while ( s < e ) {
15+ maxArea = Math . max ( ( e - s ) * Math . min ( height [ s ] , height [ e ] ) , maxArea ) ;
16+ if ( height [ s ] > height [ e ] ) {
17+ e -= 1 ;
18+ } else {
19+ s += 1 ;
20+ }
21+ }
22+ return maxArea ;
23+ }
You can’t perform that action at this time.
0 commit comments