File tree Expand file tree Collapse file tree 4 files changed +142
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 4 files changed +142
-0
lines changed Original file line number Diff line number Diff line change 1+ // v: len(vertexes), e: len(edges)
2+ // Time complexity: O(v + e)
3+ // Space complexity: O(v + e)
4+
5+ /**
6+ * // Definition for a _Node.
7+ * function _Node(val, neighbors) {
8+ * this.val = val === undefined ? 0 : val;
9+ * this.neighbors = neighbors === undefined ? [] : neighbors;
10+ * };
11+ */
12+
13+ /**
14+ * @param {_Node } node
15+ * @return {_Node }
16+ */
17+ var cloneGraph = function ( node ) {
18+ const nodes = Array . from ( { length : 101 } , ( _ , i ) => null ) ;
19+
20+ const dfs = ( node ) => {
21+ if ( ! node ) {
22+ return ;
23+ }
24+
25+ if ( nodes [ node . val ] ) {
26+ return nodes [ node . val ] ;
27+ }
28+
29+ const newNode = new _Node ( node . val ) ;
30+ nodes [ node . val ] = newNode ;
31+
32+ for ( const neighbor of node . neighbors ) {
33+ const cloned = dfs ( neighbor ) ;
34+ newNode . neighbors . push ( cloned ) ;
35+ }
36+
37+ return newNode ;
38+ } ;
39+
40+ dfs ( node ) ;
41+
42+ return nodes [ 1 ] ;
43+ } ;
Original file line number Diff line number Diff line change 1+ // n: len(text1), m: len(text2)
2+ // Time complexity: O(n * m)
3+ // Space complexity: O(n * m)
4+
5+ /**
6+ * @param {string } text1
7+ * @param {string } text2
8+ * @return {number }
9+ */
10+ var longestCommonSubsequence = function ( text1 , text2 ) {
11+ const n = text1 . length ;
12+ const m = text2 . length ;
13+
14+ const dp = Array . from ( { length : n + 1 } , ( ) =>
15+ Array . from ( { length : m + 1 } , ( ) => 0 )
16+ ) ;
17+
18+ for ( let i = 1 ; i <= n ; i ++ ) {
19+ for ( let j = 1 ; j <= m ; j ++ ) {
20+ if ( text1 [ i - 1 ] === text2 [ j - 1 ] ) {
21+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1 ;
22+ continue ;
23+ }
24+
25+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ;
26+ }
27+ }
28+
29+ return dp . at ( - 1 ) . at ( - 1 ) ;
30+ } ;
Original file line number Diff line number Diff line change 1+ // Time complexity: O(n)
2+ // Space complexity: O(1)
3+
4+ /**
5+ * @param {string } s
6+ * @param {number } k
7+ * @return {number }
8+ */
9+ var characterReplacement = function ( s , k ) {
10+ let i = 0 ;
11+ let j = 0 ;
12+
13+ const counter = new Map ( ) ;
14+ counter . set ( s [ i ] , 1 ) ;
15+
16+ let answer = 1 ;
17+
18+ const getMaxCount = ( ) => {
19+ let maxCount = 0 ;
20+
21+ for ( const [ key , value ] of counter ) {
22+ maxCount = Math . max ( maxCount , value ) ;
23+ }
24+
25+ return maxCount ;
26+ } ;
27+
28+ while ( true ) {
29+ if ( s . length - i <= answer ) {
30+ break ;
31+ }
32+
33+ const maxCount = getMaxCount ( ) ;
34+ const totalCount = j - i + 1 ;
35+
36+ if ( totalCount - maxCount <= k ) {
37+ j ++ ;
38+ counter . set ( s [ j ] , ( counter . get ( s [ j ] ) || 0 ) + 1 ) ;
39+ answer = Math . max ( totalCount , answer ) ;
40+ continue ;
41+ }
42+
43+ counter . set ( s [ i ] , counter . get ( s [ i ] ) - 1 ) ;
44+ if ( counter . get ( s [ i ] ) === 0 ) {
45+ counter . delete ( s [ i ] ) ;
46+ }
47+
48+ i ++ ;
49+ }
50+
51+ return answer ;
52+ } ;
Original file line number Diff line number Diff line change 1+ // Time complexity: O(logn)
2+ // Space complexity: O(1)
3+
4+ function hammingWeight ( n ) {
5+ let answer = 1 ;
6+ let current = n ;
7+
8+ while ( current > 1 ) {
9+ if ( current % 2 !== 0 ) {
10+ answer ++ ;
11+ }
12+
13+ current = Math . floor ( current / 2 ) ;
14+ }
15+
16+ return answer ;
17+ }
You can’t perform that action at this time.
0 commit comments