File tree Expand file tree Collapse file tree 6 files changed +160
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 6 files changed +160
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * // Definition for a Node.
3
+ * function Node(val, neighbors) {
4
+ * this.val = val === undefined ? 0 : val;
5
+ * this.neighbors = neighbors === undefined ? [] : neighbors;
6
+ * };
7
+ */
8
+
9
+ /**
10
+ * @param {Node } node
11
+ * @return {Node }
12
+ */
13
+ var cloneGraph = function ( node ) {
14
+ if ( ! node ) return null ;
15
+
16
+ const visited = new Map ( ) ;
17
+
18
+ const dfs = ( currNode ) => {
19
+ if ( visited . has ( currNode ) ) {
20
+ return visited . get ( currNode ) ;
21
+ }
22
+
23
+ // ๋
ธ๋ ๋ณต์ฌ
24
+ const clone = new Node ( currNode . val ) ;
25
+ visited . set ( currNode , clone ) ;
26
+
27
+ // ์ด์ ๋
ธ๋๋ค๋ ๋ณต์ฌํด์ ์ฐ๊ฒฐ
28
+ for ( let neighbor of currNode . neighbors ) {
29
+ clone . neighbors . push ( dfs ( neighbor ) ) ;
30
+ }
31
+
32
+ return clone ;
33
+ } ;
34
+
35
+ return dfs ( node ) ;
36
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } text1
3
+ * @param {string } text2
4
+ * @return {number }
5
+ */
6
+ var longestCommonSubsequence = function ( text1 , text2 ) {
7
+ const m = text1 . length ,
8
+ n = text2 . length ;
9
+ const dp = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ;
10
+
11
+ for ( let i = 1 ; i <= m ; i ++ ) {
12
+ for ( let j = 1 ; j <= n ; j ++ ) {
13
+ if ( text1 [ i - 1 ] === text2 [ j - 1 ] ) {
14
+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1 ;
15
+ } else {
16
+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ;
17
+ }
18
+ }
19
+ }
20
+
21
+ return dp [ m ] [ n ] ;
22
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @param {number } k
4
+ * @return {number }
5
+ */
6
+ var characterReplacement = function ( s , k ) {
7
+ let left = 0 ;
8
+ let maxCount = 0 ;
9
+ let freq = new Array ( 26 ) . fill ( 0 ) ; // A~Z
10
+
11
+ let maxLength = 0 ;
12
+
13
+ for ( let right = 0 ; right < s . length ; right ++ ) {
14
+ const idx = s . charCodeAt ( right ) - "A" . charCodeAt ( 0 ) ;
15
+ freq [ idx ] ++ ;
16
+ maxCount = Math . max ( maxCount , freq [ idx ] ) ;
17
+
18
+ let windowSize = right - left + 1 ;
19
+
20
+ if ( windowSize - maxCount > k ) {
21
+ const leftIdx = s . charCodeAt ( left ) - "A" . charCodeAt ( 0 ) ;
22
+ freq [ leftIdx ] -- ;
23
+ left ++ ;
24
+ }
25
+
26
+ maxLength = Math . max ( maxLength , right - left + 1 ) ;
27
+ }
28
+
29
+ return maxLength ;
30
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {character[][] } grid
3
+ * @return {number }
4
+ */
5
+ const numIslands = function ( grid ) {
6
+ let count = 0 ;
7
+
8
+ const dfs = ( i , j ) => {
9
+ if (
10
+ i < 0 ||
11
+ i >= grid . length ||
12
+ j < 0 ||
13
+ j >= grid [ i ] . length ||
14
+ grid [ i ] [ j ] === "0"
15
+ ) {
16
+ return ;
17
+ }
18
+
19
+ grid [ i ] [ j ] = "0" ;
20
+
21
+ dfs ( i + 1 , j ) ;
22
+ dfs ( i - 1 , j ) ;
23
+ dfs ( i , j + 1 ) ;
24
+ dfs ( i , j - 1 ) ;
25
+ } ;
26
+
27
+ for ( let i = 0 ; i < grid . length ; i ++ ) {
28
+ for ( let j = 0 ; j < grid [ i ] . length ; j ++ ) {
29
+ if ( grid [ i ] [ j ] === "1" ) {
30
+ dfs ( i , j ) ;
31
+ count ++ ;
32
+ }
33
+ }
34
+ }
35
+
36
+ return count ;
37
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+ var countSubstrings = function ( s ) {
6
+ let count = 0 ;
7
+
8
+ const expand = ( left , right ) => {
9
+ while ( left >= 0 && right < s . length && s [ left ] === s [ right ] ) {
10
+ count ++ ;
11
+ left -- ;
12
+ right ++ ;
13
+ }
14
+ } ;
15
+
16
+ for ( let i = 0 ; i < s . length ; i ++ ) {
17
+ expand ( i , i ) ; // ํ์ ๊ธธ์ด ์ค์ฌ (์: "aba")
18
+ expand ( i , i + 1 ) ; // ์ง์ ๊ธธ์ด ์ค์ฌ (์: "aa")
19
+ }
20
+
21
+ return count ;
22
+ } ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n - a positive integer
3
+ * @return {number } - reversed bits
4
+ */
5
+ var reverseBits = function ( n ) {
6
+ let result = 0 ;
7
+ for ( let i = 0 ; i < 32 ; i ++ ) {
8
+ result <<= 1 ; // ์ผ์ชฝ์ผ๋ก 1๋นํธ ์ด๋
9
+ result |= n & 1 ; // ๋ง์ง๋ง ๋นํธ ์ถ์ถํด์ ๊ฒฐ๊ณผ์ ์ถ๊ฐ
10
+ n >>>= 1 ; // ๋ถํธ ์๋ ์ฐ์ธก ์ํํธ (>>>)
11
+ }
12
+ return result >>> 0 ; // unsigned 32๋นํธ ์ ์๋ก ๋ณํ
13
+ } ;
You canโt perform that action at this time.
0 commit comments