File tree Expand file tree Collapse file tree 3 files changed +111
-0
lines changed
container-with-most-water Expand file tree Collapse file tree 3 files changed +111
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } height
3
+ * @return {number }
4
+ */
5
+ var maxArea = function ( height ) {
6
+ let max = 0 ;
7
+
8
+ let startIdx = 0 ;
9
+ let endIdx = height . length - 1 ;
10
+
11
+ while ( startIdx < endIdx ) {
12
+ const start = height [ startIdx ] ;
13
+ const end = height [ endIdx ] ;
14
+
15
+ const gap = endIdx - startIdx ;
16
+ const min = Math . min ( start , end ) ;
17
+
18
+ const area = gap * min ;
19
+
20
+ max = Math . max ( max , area ) ;
21
+
22
+ if ( start < end ) startIdx ++ ;
23
+ else endIdx -- ;
24
+ }
25
+
26
+ return max ;
27
+ } ;
28
+
29
+ // 시간복잡도 O(n)
30
+ // n은 주어진 배열(height)의 길이
31
+
32
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } matrix
3
+ * @return {number[] }
4
+ */
5
+ var spiralOrder = function ( matrix ) {
6
+ let top = 0 ;
7
+ let left = 0 ;
8
+ let bottom = matrix . length - 1 ;
9
+ let right = matrix [ 0 ] . length - 1 ;
10
+
11
+ const answer = [ ] ;
12
+
13
+ while ( top <= bottom && left <= right ) {
14
+ for ( let i = left ; i <= right ; i ++ ) {
15
+ answer . push ( matrix [ top ] [ i ] ) ;
16
+ }
17
+ top ++ ;
18
+
19
+ if ( top > bottom ) {
20
+ break ;
21
+ }
22
+
23
+ for ( let j = top ; j <= bottom ; j ++ ) {
24
+ answer . push ( matrix [ j ] [ right ] ) ;
25
+ }
26
+ right -- ;
27
+
28
+ if ( left > right ) {
29
+ break ;
30
+ }
31
+
32
+ for ( let k = right ; k >= left ; k -- ) {
33
+ answer . push ( matrix [ bottom ] [ k ] ) ;
34
+ }
35
+ bottom -- ;
36
+
37
+ for ( let l = bottom ; l >= top ; l -- ) {
38
+ answer . push ( matrix [ l ] [ left ] ) ;
39
+ }
40
+ left ++ ;
41
+ }
42
+
43
+ return answer ;
44
+ } ;
45
+
46
+ // 시간 복잡도 O(m * n)
47
+ // 공간 복잡도 O(1)
48
+
49
+
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {boolean }
4
+ */
5
+ var isValid = function ( s ) {
6
+ const stack = [ ] ;
7
+
8
+ for ( let i = 0 ; i < s . length ; i ++ ) {
9
+ const cur = s [ i ] ;
10
+
11
+ if ( cur === '(' || cur === '[' || cur === '{' ) {
12
+ stack . push ( cur ) ;
13
+ } else {
14
+ const topOfStack = stack [ stack . length - 1 ] ;
15
+
16
+ const caseA = topOfStack === '(' && cur === ')' ;
17
+ const caseB = topOfStack === '[' && cur === ']' ;
18
+ const caseC = topOfStack === '{' && cur === '}' ;
19
+
20
+ if ( caseA || caseB || caseC ) {
21
+ stack . pop ( ) ;
22
+ } else {
23
+ return false ;
24
+ }
25
+ }
26
+ }
27
+
28
+ return stack . length === 0 ;
29
+ } ;
30
+
You can’t perform that action at this time.
0 commit comments