File tree 5 files changed +150
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence
5 files changed +150
-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 result = 0
7
+ let l = 0 ;
8
+ let r = height . length - 1 ;
9
+
10
+ while ( l < r ) {
11
+ const area = ( r - l ) * Math . min ( height [ l ] , height [ r ] ) ;
12
+ result = Math . max ( result , area ) ;
13
+
14
+ if ( height [ l ] < height [ r ] ) {
15
+ l += 1 ;
16
+ } else {
17
+ r -= 1 ;
18
+ }
19
+ }
20
+ return result
21
+ } ;
Original file line number Diff line number Diff line change
1
+ class TrieNode {
2
+ constructor ( ) {
3
+ this . children = { } ;
4
+ this . word = false ;
5
+ }
6
+ }
7
+
8
+ class WordDictionary {
9
+ constructor ( ) {
10
+ this . root = new TrieNode ( ) ;
11
+ }
12
+
13
+ addWord ( word ) {
14
+ let cur = this . root ;
15
+ for ( let c of word ) {
16
+ if ( ! cur . children [ c ] ) {
17
+ cur . children [ c ] = new TrieNode ( ) ;
18
+ }
19
+ cur = cur . children [ c ] ;
20
+ }
21
+ cur . word = true ;
22
+ }
23
+
24
+ search ( word ) {
25
+ const dfs = ( j , root ) => {
26
+ let cur = root ;
27
+ for ( let i = j ; i < word . length ; i ++ ) {
28
+ const c = word [ i ] ;
29
+ if ( c === '.' ) {
30
+ for ( let child of Object . values ( cur . children ) ) {
31
+ if ( dfs ( i + 1 , child ) ) {
32
+ return true ;
33
+ }
34
+ }
35
+ return false ;
36
+ } else {
37
+ if ( ! cur . children [ c ] ) {
38
+ return false ;
39
+ }
40
+ cur = cur . children [ c ] ;
41
+ }
42
+ }
43
+ return cur . word ;
44
+ } ;
45
+ return dfs ( 0 , this . root ) ;
46
+ }
47
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[] } nums
3
+ * @return {number }
4
+ */
5
+ var lengthOfLIS = function ( nums ) {
6
+ const lis = new Array ( nums . length ) . fill ( 1 ) ;
7
+
8
+ for ( let i = nums . length - 2 ; i >= 0 ; i -- ) {
9
+ for ( let j = i + 1 ; j < nums . length ; j ++ ) {
10
+ if ( nums [ i ] < nums [ j ] ) {
11
+ lis [ i ] = Math . max ( lis [ i ] , 1 + lis [ j ] )
12
+ }
13
+ }
14
+ }
15
+ return Math . max ( ...lis )
16
+ } ;
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
+ if ( matrix . length === 0 ) return [ ] ;
7
+
8
+ const res = [ ] ;
9
+ let left = 0 , right = matrix [ 0 ] . length ;
10
+ let top = 0 , bottom = matrix . length ;
11
+
12
+ while ( left < right && top < bottom ) {
13
+ // 상단 행 왼쪽 → 오른쪽
14
+ for ( let i = left ; i < right ; i ++ ) {
15
+ res . push ( matrix [ top ] [ i ] ) ;
16
+ }
17
+ top += 1 ;
18
+
19
+ // 오른쪽 열 위 → 아래
20
+ for ( let i = top ; i < bottom ; i ++ ) {
21
+ res . push ( matrix [ i ] [ right - 1 ] ) ;
22
+ }
23
+ right -= 1 ;
24
+
25
+ if ( ! ( left < right && top < bottom ) ) break ;
26
+
27
+ // 하단 행 오른쪽 → 왼쪽
28
+ for ( let i = right - 1 ; i >= left ; i -- ) {
29
+ res . push ( matrix [ bottom - 1 ] [ i ] ) ;
30
+ }
31
+ bottom -= 1 ;
32
+
33
+ // 왼쪽 열 아래 → 위
34
+ for ( let i = bottom - 1 ; i >= top ; i -- ) {
35
+ res . push ( matrix [ i ] [ left ] ) ;
36
+ }
37
+ left += 1 ;
38
+ }
39
+
40
+ return res ;
41
+ } ;
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
+ const pair = {
9
+ ')' : '(' ,
10
+ '}' : '{' ,
11
+ ']' : '['
12
+ }
13
+
14
+ for ( let item of s ) {
15
+ // 여는 괄호
16
+ if ( item === '(' || item === '{' || item === '[' ) {
17
+ stack . push ( item )
18
+ // stack길이가 0 or 닫는 괄호 케이스
19
+ } else if ( stack . length === 0 || stack . pop ( ) !== pair [ item ] ) {
20
+ return false
21
+ }
22
+ }
23
+
24
+ return stack . length === 0
25
+ } ;
You can’t perform that action at this time.
0 commit comments