File tree 3 files changed +58
-0
lines changed
longest-common-subsequence
3 files changed +58
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * 가장 긴 공통 부분 수열 구하기
3
+ * 알고리즘 복잡도
4
+ * - 시간 복잡도: O(m*n)
5
+ * - 공간 복잡도: O(m*n)
6
+ * @param text1
7
+ * @param text2
8
+ */
9
+ function longestCommonSubsequence ( text1 : string , text2 : string ) : number {
10
+ let dp : number [ ] [ ] = Array ( text1 . length + 1 ) . fill ( 0 )
11
+ . map ( ( ) => Array ( text2 . length + 1 ) . fill ( 0 ) ) ;
12
+
13
+ for ( let i = 1 ; i <= text1 . length ; i ++ ) {
14
+ for ( let j = 1 ; j <= text2 . length ; j ++ ) {
15
+ if ( text1 [ i - 1 ] === text2 [ j - 1 ] ) {
16
+ dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1 ;
17
+ } else {
18
+ dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ;
19
+ }
20
+ }
21
+ }
22
+
23
+ return dp [ text1 . length ] [ text2 . length ] ;
24
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 10진수 n이 2진수일 때 1을 세기
3
+ * 알고리즘 복잡도
4
+ * - 시간 복잡도: O(logn)
5
+ * - 공간 복잡도: O(logn)
6
+ * @param n
7
+ */
8
+ function hammingWeight ( n : number ) : number {
9
+ let binary = "" ;
10
+
11
+ while ( n > 0 ) {
12
+ binary = ( n % 2 ) + binary ;
13
+ n = Math . floor ( n / 2 ) ;
14
+ }
15
+
16
+ return [ ...binary ] . filter ( val => val === '1' ) . length
17
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 연산자 + 사용하지 않고 덧셈하기
3
+ * 알고리즘 복잡도
4
+ * - 시간 복잡도: O(logn) - 비트 수만큼 계산
5
+ * - 공간 복잡도: O(1)
6
+ * @param a
7
+ * @param b
8
+ */
9
+ function getSum ( a : number , b : number ) : number {
10
+ while ( b !== 0 ) {
11
+ let carry = a & b ; // and - 11 & 10 = 10
12
+ a = a ^ b ; // xor - 11 ^ 10 = 01
13
+ b = carry << 1 ; // 100
14
+ }
15
+
16
+ return a
17
+ }
You can’t perform that action at this time.
0 commit comments