File tree Expand file tree Collapse file tree 5 files changed +165
-0
lines changed
longest-common-subsequence
longest-repeating-character-replacement Expand file tree Collapse file tree 5 files changed +165
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for _Node.
3
+ * class _Node {
4
+ * val: number
5
+ * neighbors: _Node[]
6
+ *
7
+ * constructor(val?: number, neighbors?: _Node[]) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.neighbors = (neighbors===undefined ? [] : neighbors)
10
+ * }
11
+ * }
12
+ *
13
+ */
14
+
15
+ function cloneGraph ( node : _Node | null ) : _Node | null {
16
+ if ( ! node ) return null ;
17
+
18
+ const cloned = new Map < number , _Node > ( ) ;
19
+
20
+ const queue : _Node [ ] = [ ] ;
21
+
22
+ const copied = new _Node ( node . val ) ;
23
+ cloned . set ( node . val , copied ) ;
24
+
25
+ queue . push ( node ) ;
26
+
27
+ let pointer = 0 ;
28
+
29
+ while ( pointer < queue . length ) {
30
+ const current = queue [ pointer ++ ] ;
31
+
32
+ const copiedNode = cloned . get ( current . val ) ! ;
33
+
34
+ for ( const neighbor of current . neighbors ) {
35
+ if ( ! cloned . has ( neighbor . val ) ) {
36
+ const copiedNeighbor = new _Node ( neighbor . val ) ;
37
+ cloned . set ( neighbor . val , copiedNeighbor ) ;
38
+
39
+ queue . push ( neighbor ) ;
40
+ }
41
+ copiedNode . neighbors . push ( cloned . get ( neighbor . val ) ! ) ;
42
+ }
43
+ }
44
+
45
+ return copied ;
46
+ }
Original file line number Diff line number Diff line change
1
+ function longestCommonSubsequence ( text1 : string , text2 : string ) : number {
2
+ const m = text1 . length ;
3
+ const n = text2 . length ;
4
+
5
+ const dp : number [ ] [ ] = Array . from ( { length : m + 1 } , ( ) => Array ( n + 1 ) . fill ( 0 ) ) ;
6
+
7
+ for ( let i = 1 ; i <= m ; i ++ ) {
8
+ for ( let j = 1 ; j <= n ; j ++ ) {
9
+ if ( text1 [ i - 1 ] === text2 [ j - 1 ] ) {
10
+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1 ;
11
+ } else {
12
+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ;
13
+ }
14
+ }
15
+ }
16
+
17
+ return dp [ m ] [ n ] ;
18
+ }
Original file line number Diff line number Diff line change
1
+ function characterReplacement ( s : string , k : number ) : number {
2
+ let longestLength = 0 ;
3
+ let windowStart = 0 ;
4
+
5
+ const countMap = new Map < string , number > ( ) ;
6
+ let maxCount = 0 ;
7
+
8
+ for ( let windowEnd = 0 ; windowEnd < s . length ; windowEnd ++ ) {
9
+ const currentCh = s [ windowEnd ] ;
10
+ countMap . set ( currentCh , ( countMap . get ( currentCh ) || 0 ) + 1 ) ;
11
+
12
+ maxCount = Math . max ( maxCount , countMap . get ( currentCh ) || 0 ) ;
13
+ if ( windowEnd - windowStart + 1 - maxCount > k ) {
14
+ countMap . set ( s [ windowStart ] , ( countMap . get ( s [ windowStart ] ) || 0 ) - 1 ) ;
15
+
16
+ windowStart ++ ;
17
+ }
18
+ longestLength = Math . max ( longestLength , windowEnd - windowStart + 1 ) ;
19
+ }
20
+
21
+ return longestLength ;
22
+ }
Original file line number Diff line number Diff line change
1
+ function countSubstrings ( s : string ) : number {
2
+ const sLen = s . length ;
3
+
4
+ const dp : boolean [ ] [ ] = Array . from ( { length : sLen } , ( ) => Array ( sLen ) . fill ( false ) ) ;
5
+
6
+ // 길이가 1
7
+ for ( let i = 0 ; i < sLen ; i ++ ) {
8
+ dp [ i ] [ i ] = true ;
9
+ }
10
+
11
+ // 길이가 2
12
+ for ( let i = 0 ; i < sLen - 1 ; i ++ ) {
13
+ dp [ i ] [ i + 1 ] = s [ i ] === s [ i + 1 ] ;
14
+ }
15
+
16
+ // 길이가 3이상
17
+ for ( let len = 3 ; len <= sLen ; len ++ ) {
18
+ for ( let i = 0 ; i < sLen - len + 1 ; i ++ ) {
19
+ const j = i + len - 1 ;
20
+ if ( s [ i ] === s [ j ] && dp [ i + 1 ] [ j - 1 ] ) {
21
+ dp [ i ] [ j ] = true ;
22
+ }
23
+ }
24
+ }
25
+
26
+ let count = 0 ;
27
+ for ( let i = 0 ; i < dp . length ; i ++ ) {
28
+ for ( let j = 0 ; j < dp [ i ] . length ; j ++ ) {
29
+ if ( dp [ i ] [ j ] ) count ++ ;
30
+ }
31
+ }
32
+
33
+ return count ;
34
+ }
Original file line number Diff line number Diff line change
1
+ // 8비트로 쪼갠 방법
2
+ function reverseBits ( n : number ) : number {
3
+ const TOTAL_BITS = 32 ;
4
+ const BITS_IN_BYTE = 8 ;
5
+ const BYTES_IN_INTEGER = TOTAL_BITS / BITS_IN_BYTE ;
6
+ const BYTE_MASK = 0xff ;
7
+
8
+ let result = 0 ;
9
+
10
+ const reversedByteCache : number [ ] = Array . from ( { length : 2 ** BITS_IN_BYTE } , ( _ , n ) => {
11
+ let reversedNum = 0 ;
12
+
13
+ for ( let i = 0 ; i < BITS_IN_BYTE ; i ++ ) {
14
+ const bit = ( n >>> i ) & 1 ;
15
+ reversedNum |= bit << ( BITS_IN_BYTE - 1 - i ) ;
16
+ }
17
+
18
+ return reversedNum ;
19
+ } ) ;
20
+
21
+ for ( let i = 0 ; i < BYTES_IN_INTEGER ; i ++ ) {
22
+ const byte = ( n >>> ( i * BITS_IN_BYTE ) ) & BYTE_MASK ;
23
+
24
+ const reversed = reversedByteCache [ byte ] ;
25
+ result |= reversed << ( TOTAL_BITS - ( i + 1 ) * BITS_IN_BYTE ) ;
26
+ }
27
+
28
+ return result >>> 0 ;
29
+ }
30
+
31
+ // 32번 연산 방법
32
+ function reverseBits ( n : number ) : number {
33
+ const BITS_LEN = 32 ;
34
+
35
+ let reversed = 0 ;
36
+
37
+ for ( let i = 0 ; i < BITS_LEN ; i ++ ) {
38
+ const bit = ( n >>> i ) & 1 ;
39
+
40
+ const reversedPosition = 31 - i ;
41
+ reversed |= bit << reversedPosition ;
42
+ }
43
+
44
+ return reversed >>> 0 ;
45
+ }
You can’t perform that action at this time.
0 commit comments