File tree Expand file tree Collapse file tree 4 files changed +110
-0
lines changed
longest-repeating-character-replacement Expand file tree Collapse file tree 4 files changed +110
-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 stack = [ node ] ;
17
+ const map = new Map ( ) ;
18
+
19
+ map . set ( node . val , new _Node ( node . val ) ) ;
20
+
21
+ while ( stack . length > 0 ) {
22
+ const currentNode = stack . pop ( ) ;
23
+
24
+ for ( const neighbor of currentNode . neighbors ) {
25
+ if ( ! map . has ( neighbor . val ) ) {
26
+ map . set ( neighbor . val , new Node ( neighbor . val ) ) ;
27
+ stack . push ( neighbor ) ;
28
+ }
29
+ map . get ( currentNode . val ) . neighbors . push ( map . get ( neighbor . val ) ) ;
30
+ }
31
+
32
+ }
33
+ return map . get ( node . val ) ;
34
+ } ;
35
+
36
+
37
+ // 시간복잡도: O(n)
38
+ // 공간복잡도: O(n)
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 start = 0 ;
8
+ let maxFreq = 0 ;
9
+ let result = 0 ;
10
+ const map = new Map ( ) ;
11
+
12
+ for ( let end = 0 ; end < s . length ; end ++ ) {
13
+ const endChar = s [ end ] ;
14
+ map . set ( endChar , 1 + ( map . get ( endChar ) || 0 ) ) ;
15
+
16
+ maxFreq = Math . max ( maxFreq , map . get ( endChar ) ) ;
17
+
18
+ if ( end - start + 1 - maxFreq > k ) {
19
+ const startChar = s [ start ] ;
20
+ map . set ( startChar , map . get ( startChar ) - 1 ) ;
21
+ start ++ ;
22
+
23
+ }
24
+
25
+ result = Math . max ( result , end - start + 1 ) ;
26
+ }
27
+
28
+ return result ;
29
+ } ;
30
+
31
+ // 시간복잡도: O(n)
32
+ // 공간복잡도: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {number }
4
+ */
5
+
6
+ var countSubstrings = function ( s ) {
7
+ let count = 0 ;
8
+ for ( let i = 0 ; i < s . length ; i ++ ) {
9
+ let start = i ;
10
+ let end = i ;
11
+ while ( start >= 0 && end < s . length && s [ start ] === s [ end ] ) {
12
+ count += 1 ;
13
+ start -= 1 ;
14
+ end += 1 ;
15
+ }
16
+
17
+ start = i ;
18
+ end = i + 1 ;
19
+ while ( start >= 0 && end < s . length && s [ start ] === s [ end ] ) {
20
+ count += 1 ;
21
+ start -= 1 ;
22
+ end += 1 ;
23
+ }
24
+ }
25
+
26
+ return count ;
27
+ } ;
28
+
29
+ // 시간 복잡도: O(n^2)
30
+ // 공간 복잡도: O(1)
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n - a positive integer
3
+ * @return {number } - a positive integer
4
+ */
5
+ var reverseBits = function ( n ) {
6
+ return parseInt ( n . toString ( 2 ) . padStart ( 32 , '0' ) . split ( '' ) . reverse ( ) . join ( '' ) , 2 ) ;
7
+ } ;
8
+
9
+ // 시간복잡도: O(n)
10
+ // 공간복잡도: O(1)
You can’t perform that action at this time.
0 commit comments