File tree 5 files changed +170
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence
5 files changed +170
-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 left = 0 ;
7
+ let right = height . length - 1 ;
8
+ let max = 0 ;
9
+
10
+ while ( right > left ) {
11
+ if ( height [ left ] >= height [ right ] ) {
12
+ max = Math . max ( max , height [ right ] * ( right - left ) ) ;
13
+ right = right - 1 ;
14
+ }
15
+ else {
16
+ max = Math . max ( max , height [ left ] * ( right - left ) ) ;
17
+ left = left + 1 ;
18
+ }
19
+ }
20
+ return max ;
21
+ } ;
Original file line number Diff line number Diff line change
1
+ class TrieNode {
2
+ constructor ( ) {
3
+ this . children = { } ; // 문자 -> TrieNode 매핑
4
+ this . isEnd = false ; // 단어의 끝 표시
5
+ }
6
+ }
7
+
8
+ class WordDictionary {
9
+ constructor ( ) {
10
+ this . root = new TrieNode ( ) ;
11
+ }
12
+
13
+ /**
14
+ * 단어를 트라이에 추가
15
+ * Time Complexity: O(L) // L = word.length
16
+ * Space Complexity: O(L) // 새로운 노드 최대 L개 추가 가능
17
+ */
18
+ addWord ( word ) {
19
+ let node = this . root ;
20
+ for ( const char of word ) {
21
+ if ( ! node . children [ char ] ) {
22
+ node . children [ char ] = new TrieNode ( ) ;
23
+ }
24
+ node = node . children [ char ] ;
25
+ }
26
+ node . isEnd = true ;
27
+ }
28
+
29
+ /**
30
+ * 단어 또는 패턴 검색
31
+ * Time Complexity: O(26^D * L) // D = '.' 개수 (최대 2), L = word.length
32
+ * Space Complexity: O(L) // DFS 재귀 호출 스택 깊이
33
+ */
34
+ search ( word ) {
35
+ const dfs = ( index , node ) => {
36
+ for ( let i = index ; i < word . length ; i ++ ) {
37
+ const char = word [ i ] ;
38
+ if ( char === '.' ) {
39
+ for ( const child of Object . values ( node . children ) ) {
40
+ if ( dfs ( i + 1 , child ) ) return true ;
41
+ }
42
+ return false ;
43
+ } else {
44
+ if ( ! node . children [ char ] ) return false ;
45
+ node = node . children [ char ] ;
46
+ }
47
+ }
48
+ return node . isEnd ;
49
+ } ;
50
+
51
+ return dfs ( 0 , this . root ) ;
52
+ }
53
+ }
Original file line number Diff line number Diff line change
1
+ // Time: O(n log n), Space: O(n)
2
+ function lengthOfLIS ( nums ) {
3
+ const tails = [ ] ;
4
+
5
+ for ( const num of nums ) {
6
+ let left = 0 , right = tails . length ;
7
+
8
+ // 이진 탐색: tails에서 num이 들어갈 최소 위치 찾기
9
+ while ( left < right ) {
10
+ const mid = Math . floor ( ( left + right ) / 2 ) ;
11
+ if ( tails [ mid ] < num ) {
12
+ left = mid + 1 ;
13
+ } else {
14
+ right = mid ;
15
+ }
16
+ }
17
+
18
+ // left는 삽입 위치
19
+ tails [ left ] = num ;
20
+ }
21
+
22
+ return tails . length ;
23
+ }
Original file line number Diff line number Diff line change
1
+ // Time: O(m * n)
2
+ // Space: O(1) + output array
3
+ function spiralOrder ( matrix ) {
4
+ const result = [ ] ;
5
+
6
+ let top = 0 ;
7
+ let bottom = matrix . length - 1 ;
8
+ let left = 0 ;
9
+ let right = matrix [ 0 ] . length - 1 ;
10
+
11
+ while ( top <= bottom && left <= right ) {
12
+ // 1. 왼 → 오
13
+ for ( let i = left ; i <= right ; i ++ ) {
14
+ result . push ( matrix [ top ] [ i ] ) ;
15
+ }
16
+ top ++ ;
17
+
18
+ // 2. 위 → 아래
19
+ for ( let i = top ; i <= bottom ; i ++ ) {
20
+ result . push ( matrix [ i ] [ right ] ) ;
21
+ }
22
+ right -- ;
23
+
24
+ // 3. 오 → 왼
25
+ if ( top <= bottom ) {
26
+ for ( let i = right ; i >= left ; i -- ) {
27
+ result . push ( matrix [ bottom ] [ i ] ) ;
28
+ }
29
+ bottom -- ;
30
+ }
31
+
32
+ // 4. 아래 → 위
33
+ if ( left <= right ) {
34
+ for ( let i = bottom ; i >= top ; i -- ) {
35
+ result . push ( matrix [ i ] [ left ] ) ;
36
+ }
37
+ left ++ ;
38
+ }
39
+ }
40
+
41
+ return result ;
42
+ }
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
+ let ch = { } ;
7
+ ch [ ')' ] = '(' ;
8
+ ch [ '}' ] = '{' ;
9
+ ch [ ']' ] = '[' ;
10
+
11
+ let list = s . split ( '' ) ;
12
+ let stack = [ ] ;
13
+
14
+ list . forEach ( ( ele ) => {
15
+ if ( stack . length == 0 ) {
16
+ stack . push ( ele ) ;
17
+ } else {
18
+ let len = stack . length ;
19
+ if ( stack [ len - 1 ] == ch [ ele ] ) {
20
+ stack . pop ( ) ;
21
+ } else {
22
+ stack . push ( ele ) ;
23
+ }
24
+ }
25
+ } )
26
+ if ( stack . length == 0 ) {
27
+ return true ;
28
+ } else {
29
+ return false ;
30
+ }
31
+ } ;
You can’t perform that action at this time.
0 commit comments