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
+ * ์ฃผ์ด์ง ๋ฐฐ์ด์์ (๋ ์์ ์ค ์์ ๊ฐ) * (๋ ์์์ ์ธ๋ฑ์ค ์ฐจ์ด)์ ์ต๋๊ฐ์ ๋ฐํํ๋ ํจ์
3
+ * @param {number[] } height
4
+ * @return {number }
5
+ */
6
+ const maxArea = function ( height ) {
7
+ let left = 0 ;
8
+ let right = height . length - 1 ;
9
+ let max = 0 ;
10
+
11
+ while ( left < right ) {
12
+ const w = right - left ;
13
+ const h = Math . min ( height [ left ] , height [ right ] ) ;
14
+
15
+ max = Math . max ( max , w * h ) ;
16
+
17
+ if ( height [ left ] <= height [ right ] ) {
18
+ left ++ ;
19
+ } else {
20
+ right -- ;
21
+ }
22
+ }
23
+
24
+ return max ;
25
+ } ;
26
+
27
+ // ์๊ฐ๋ณต์ก๋: O(n)
28
+ // ๊ณต๊ฐ๋ณต์ก๋: O(1)
Original file line number Diff line number Diff line change
1
+ class TrieNode {
2
+ constructor ( value ) {
3
+ this . value = value ;
4
+ this . children = { } ;
5
+ this . end = false ;
6
+ }
7
+ }
8
+
9
+ class WordDictionary {
10
+ constructor ( ) {
11
+ this . root = new TrieNode ( null ) ;
12
+ }
13
+
14
+ /**
15
+ * ์๊ฐ๋ณต์ก๋: O(w) (w: word.length)
16
+ * ๊ณต๊ฐ๋ณต์ก๋: O(w)
17
+ * @param {string } word
18
+ * @return {void }
19
+ */
20
+ addWord ( word ) {
21
+ let current = this . root ;
22
+
23
+ for ( const char of word ) {
24
+ if ( ! current . children [ char ] ) {
25
+ const child = new TrieNode ( char ) ;
26
+ current . children [ char ] = child ;
27
+ }
28
+ current = current . children [ char ] ;
29
+ }
30
+
31
+ current . end = true ;
32
+ } ;
33
+
34
+ /**
35
+ * ์๊ฐ๋ณต์ก๋: O(k * w) (k: children์ ๊ธธ์ด๋ก ์ต๋ 26, w: word.length)
36
+ * ๊ณต๊ฐ๋ณต์ก๋: O(w)
37
+ * @param {string } word
38
+ * @return {boolean }
39
+ */
40
+ search ( word ) {
41
+ return this . #search( this . root , word , 0 ) ;
42
+ } ;
43
+
44
+ #search( node , word , idx ) {
45
+ if ( ! node ) return false ;
46
+ if ( idx >= word . length ) return node . end ;
47
+
48
+ if ( word [ idx ] === '.' ) {
49
+ for ( const current of Object . values ( node . children ) ) {
50
+ if ( this . #search( current , word , idx + 1 ) ) {
51
+ return true ;
52
+ }
53
+ }
54
+ return false ;
55
+ } else {
56
+ return this . #search( node . children [ word [ idx ] ] , word , idx + 1 ) ;
57
+ }
58
+ }
59
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ๋ฐฐ์ด์์ ๊ฐ์ฅ ๊ธด ์ฆ๊ฐํ๋ ์์ด์ ๊ธธ์ด๋ฅผ ๋ฐํํ๋ ํจ์
3
+ * @param {number[] } nums
4
+ * @return {number }
5
+ */
6
+ const lengthOfLIS = function ( nums ) {
7
+ const dp = Array ( nums . length ) . fill ( 1 ) ;
8
+
9
+ for ( let i = 0 ; i < nums . length ; i ++ ) {
10
+ for ( let j = 0 ; j < i ; j ++ ) {
11
+ if ( nums [ j ] < nums [ i ] )
12
+ dp [ i ] = Math . max ( dp [ j ] + 1 , dp [ i ] ) ;
13
+ }
14
+ }
15
+
16
+ return Math . max ( ...dp ) ;
17
+ } ;
18
+
19
+ // ์๊ฐ๋ณต์ก๋: O(n^2)
20
+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ํ๋ ฌ์ ๋์ ํ(์ฐ-ํ-์ข-์)์ผ๋ก ์ํํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ ํจ์
3
+ * @param {number[][] } matrix
4
+ * @return {number[] }
5
+ */
6
+ const spiralOrder = function ( matrix ) {
7
+ const rows = matrix . length ;
8
+ const cols = matrix [ 0 ] . length ;
9
+ let r = 0 ;
10
+ let c = 0 ;
11
+ let dr = 0 ; // 0, 1, 0, -1
12
+ let dc = 1 ; // 1, 0, -1, 0
13
+
14
+ const output = [ ] ;
15
+
16
+ for ( let i = 0 ; i < rows * cols ; i ++ ) {
17
+ output . push ( matrix [ r ] [ c ] ) ;
18
+ matrix [ r ] [ c ] = null ;
19
+
20
+ // ๋ฐฉํฅ์ ์ ํํด์ผ ํ๋ ๊ฒฝ์ฐ
21
+ if ( ! ( 0 <= r + dr && r + dr < rows && 0 <= c + dc && c + dc < cols ) || matrix [ r + dr ] [ c + dc ] === null ) {
22
+ [ dr , dc ] = [ dc , - dr ] ;
23
+ }
24
+
25
+ r += dr ;
26
+ c += dc ;
27
+ }
28
+
29
+ return output ;
30
+ } ;
31
+
32
+ // ์๊ฐ๋ณต์ก๋: O(r * c)
33
+ // ๊ณต๊ฐ๋ณต์ก๋: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์ฃผ์ด์ง ๋ฌธ์์ด์ ๊ดํธ ์์ด ์๋ง๋์ง ๋ฐํํ๋ ํจ์
3
+ * @param {string } s
4
+ * @return {boolean }
5
+ */
6
+ const isValid = function ( s ) {
7
+ const stack = [ ] ;
8
+ const pairs = {
9
+ '(' : ')' ,
10
+ '{' : '}' ,
11
+ '[' : ']' ,
12
+ }
13
+
14
+ for ( const bracket of s ) {
15
+ if ( bracket in pairs ) {
16
+ stack . push ( bracket ) ;
17
+ continue ;
18
+ }
19
+
20
+ const popped = stack . pop ( ) ;
21
+ if ( bracket !== pairs [ popped ] ) {
22
+ return false ;
23
+ }
24
+ }
25
+
26
+ return stack . length === 0 ;
27
+ } ;
28
+
29
+ // ์๊ฐ๋ณต์ก๋: O(n)
30
+ // ๊ณต๊ฐ๋ณต์ก๋: O(n)
You canโt perform that action at this time.
0 commit comments