File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ // 1번째 풀이
2+ function maxArea1 ( height : number [ ] ) : number {
3+ let left = 0 ;
4+ let right = height . length - 1 ;
5+ const area : number [ ] = [ ] ;
6+
7+ while ( left < right ) {
8+ const x = right - left ;
9+ const y = Math . min ( height [ left ] , height [ right ] ) ;
10+ area . push ( x * y ) ;
11+
12+ if ( height [ left ] < height [ right ] ) {
13+ left ++ ;
14+ } else {
15+ right -- ;
16+ }
17+ }
18+
19+ return Math . max ( ...area ) ;
20+ } ;
21+
22+ // 2번째 풀이
23+ function maxArea2 ( height : number [ ] ) : number {
24+ let left = 0 ;
25+ let right = height . length - 1 ;
26+ let max = 0 ;
27+
28+ while ( left < right ) {
29+ const x = right - left ;
30+ const y = Math . min ( height [ left ] , height [ right ] ) ;
31+ const current = x * y ;
32+ max = Math . max ( max , current ) ;
33+
34+ if ( height [ left ] < height [ right ] ) {
35+ left ++ ;
36+ } else {
37+ right -- ;
38+ }
39+ }
40+
41+ return max ;
42+ } ;
You can’t perform that action at this time.
0 commit comments