diff --git a/longest-common-subsequence/YeomChaeeun.ts b/longest-common-subsequence/YeomChaeeun.ts new file mode 100644 index 000000000..1a4dd624a --- /dev/null +++ b/longest-common-subsequence/YeomChaeeun.ts @@ -0,0 +1,24 @@ +/** + * 가장 긴 공통 부분 수열 구하기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(m*n) + * - 공간 복잡도: O(m*n) + * @param text1 + * @param text2 + */ +function longestCommonSubsequence(text1: string, text2: string): number { + let dp: number[][] = Array(text1.length + 1).fill(0) + .map(() => Array(text2.length + 1).fill(0)); + + for(let i = 1; i <= text1.length; i++) { + for(let j = 1; j <= text2.length; j++) { + if(text1[i-1] === text2[j-1]) { + dp[i][j] = dp[i-1][j-1] + 1; + } else { + dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]); + } + } + } + + return dp[text1.length][text2.length]; +} diff --git a/number-of-1-bits/YeomChaeeun.ts b/number-of-1-bits/YeomChaeeun.ts new file mode 100644 index 000000000..d1fbec020 --- /dev/null +++ b/number-of-1-bits/YeomChaeeun.ts @@ -0,0 +1,17 @@ +/** + * 10진수 n이 2진수일 때 1을 세기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(logn) + * - 공간 복잡도: O(logn) + * @param n + */ +function hammingWeight(n: number): number { + let binary = ""; + + while(n > 0) { + binary = (n % 2) + binary; + n = Math.floor(n / 2); + } + + return [...binary].filter(val => val === '1').length +} diff --git a/sum-of-two-integers/YeomChaeeun.ts b/sum-of-two-integers/YeomChaeeun.ts new file mode 100644 index 000000000..e386ae666 --- /dev/null +++ b/sum-of-two-integers/YeomChaeeun.ts @@ -0,0 +1,17 @@ +/** + * 연산자 + 사용하지 않고 덧셈하기 + * 알고리즘 복잡도 + * - 시간 복잡도: O(logn) - 비트 수만큼 계산 + * - 공간 복잡도: O(1) + * @param a + * @param b + */ +function getSum(a: number, b: number): number { + while(b !== 0) { + let carry = a & b; // and - 11 & 10 = 10 + a = a ^ b; // xor - 11 ^ 10 = 01 + b = carry << 1; // 100 + } + + return a +}