diff --git a/clone-graph/hyer0705.ts b/clone-graph/hyer0705.ts new file mode 100644 index 000000000..c00e61103 --- /dev/null +++ b/clone-graph/hyer0705.ts @@ -0,0 +1,46 @@ +/** + * Definition for _Node. + * class _Node { + * val: number + * neighbors: _Node[] + * + * constructor(val?: number, neighbors?: _Node[]) { + * this.val = (val===undefined ? 0 : val) + * this.neighbors = (neighbors===undefined ? [] : neighbors) + * } + * } + * + */ + +function cloneGraph(node: _Node | null): _Node | null { + if (!node) return null; + + const cloned = new Map(); + + const queue: _Node[] = []; + + const copied = new _Node(node.val); + cloned.set(node.val, copied); + + queue.push(node); + + let pointer = 0; + + while (pointer < queue.length) { + const current = queue[pointer++]; + + const copiedNode = cloned.get(current.val)!; + + for (const neighbor of current.neighbors) { + if (!cloned.has(neighbor.val)) { + const copiedNeighbor = new _Node(neighbor.val); + cloned.set(neighbor.val, copiedNeighbor); + + queue.push(neighbor); + } + copiedNode.neighbors.push(cloned.get(neighbor.val)!); + } + } + + return copied; +} diff --git a/longest-common-subsequence/hyer0705.ts b/longest-common-subsequence/hyer0705.ts new file mode 100644 index 000000000..db9f2944b --- /dev/null +++ b/longest-common-subsequence/hyer0705.ts @@ -0,0 +1,18 @@ +function longestCommonSubsequence(text1: string, text2: string): number { + const m = text1.length; + const n = text2.length; + + const dp: number[][] = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)); + + for (let i = 1; i <= m; i++) { + for (let j = 1; j <= n; 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[m][n]; +} diff --git a/longest-repeating-character-replacement/hyer0705.ts b/longest-repeating-character-replacement/hyer0705.ts new file mode 100644 index 000000000..bbe997e2b --- /dev/null +++ b/longest-repeating-character-replacement/hyer0705.ts @@ -0,0 +1,22 @@ +function characterReplacement(s: string, k: number): number { + let longestLength = 0; + let windowStart = 0; + + const countMap = new Map(); + let maxCount = 0; + + for (let windowEnd = 0; windowEnd < s.length; windowEnd++) { + const currentCh = s[windowEnd]; + countMap.set(currentCh, (countMap.get(currentCh) || 0) + 1); + + maxCount = Math.max(maxCount, countMap.get(currentCh) || 0); + if (windowEnd - windowStart + 1 - maxCount > k) { + countMap.set(s[windowStart], (countMap.get(s[windowStart]) || 0) - 1); + + windowStart++; + } + longestLength = Math.max(longestLength, windowEnd - windowStart + 1); + } + + return longestLength; +} diff --git a/palindromic-substrings/hyer0705.ts b/palindromic-substrings/hyer0705.ts new file mode 100644 index 000000000..0b206f071 --- /dev/null +++ b/palindromic-substrings/hyer0705.ts @@ -0,0 +1,34 @@ +function countSubstrings(s: string): number { + const sLen = s.length; + + const dp: boolean[][] = Array.from({ length: sLen }, () => Array(sLen).fill(false)); + + // 길이가 1 + for (let i = 0; i < sLen; i++) { + dp[i][i] = true; + } + + // 길이가 2 + for (let i = 0; i < sLen - 1; i++) { + dp[i][i + 1] = s[i] === s[i + 1]; + } + + // 길이가 3이상 + for (let len = 3; len <= sLen; len++) { + for (let i = 0; i < sLen - len + 1; i++) { + const j = i + len - 1; + if (s[i] === s[j] && dp[i + 1][j - 1]) { + dp[i][j] = true; + } + } + } + + let count = 0; + for (let i = 0; i < dp.length; i++) { + for (let j = 0; j < dp[i].length; j++) { + if (dp[i][j]) count++; + } + } + + return count; +} diff --git a/reverse-bits/hyer0705.ts b/reverse-bits/hyer0705.ts new file mode 100644 index 000000000..762922e6b --- /dev/null +++ b/reverse-bits/hyer0705.ts @@ -0,0 +1,45 @@ +// 8비트로 쪼갠 방법 +function reverseBits(n: number): number { + const TOTAL_BITS = 32; + const BITS_IN_BYTE = 8; + const BYTES_IN_INTEGER = TOTAL_BITS / BITS_IN_BYTE; + const BYTE_MASK = 0xff; + + let result = 0; + + const reversedByteCache: number[] = Array.from({ length: 2 ** BITS_IN_BYTE }, (_, n) => { + let reversedNum = 0; + + for (let i = 0; i < BITS_IN_BYTE; i++) { + const bit = (n >>> i) & 1; + reversedNum |= bit << (BITS_IN_BYTE - 1 - i); + } + + return reversedNum; + }); + + for (let i = 0; i < BYTES_IN_INTEGER; i++) { + const byte = (n >>> (i * BITS_IN_BYTE)) & BYTE_MASK; + + const reversed = reversedByteCache[byte]; + result |= reversed << (TOTAL_BITS - (i + 1) * BITS_IN_BYTE); + } + + return result >>> 0; +} + +// 32번 연산 방법 +function reverseBits(n: number): number { + const BITS_LEN = 32; + + let reversed = 0; + + for (let i = 0; i < BITS_LEN; i++) { + const bit = (n >>> i) & 1; + + const reversedPosition = 31 - i; + reversed |= bit << reversedPosition; + } + + return reversed >>> 0; +}