diff --git a/clone-graph/Blossssom.ts b/clone-graph/Blossssom.ts new file mode 100644 index 0000000000..d0635c61b3 --- /dev/null +++ b/clone-graph/Blossssom.ts @@ -0,0 +1,80 @@ +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 visited = new Map<_Node, _Node>(); + + visited.set(node, new _Node(node.val)); + + const queue: _Node[] = [node]; + + while (queue.length > 0) { + const currentNode = queue.shift()!; + + for (const n of currentNode?.neighbors) { + if (!visited.has(n)) { + visited.set(n, new _Node(n.val)); + queue.push(n); + } + + visited.get(currentNode)!.neighbors.push(visited.get(n)!); + } + } + + return null; +} + +// function cloneGraph(node: _Node | null): _Node | null { +// if (!node) { +// return null; +// } + +// const checkMap = new Map(); + +// function dfs(targetNode: _Node) { +// if (checkMap.has(targetNode.val)) { +// return checkMap.get(targetNode.val)!; +// } + +// const newNode = new _Node(targetNode.val); +// checkMap.set(targetNode.val, newNode); + +// for (const n of targetNode.neighbors) { +// newNode.neighbors.push(dfs(n)); +// } + +// return newNode; +// } + +// const result = dfs(node); + +// return result; +// } + +const node1 = new _Node(1); +const node2 = new _Node(2); +const node3 = new _Node(3); +const node4 = new _Node(4); + +node1.neighbors = [node2, node4]; +node2.neighbors = [node1, node3]; +node3.neighbors = [node2, node4]; +node4.neighbors = [node1, node3]; + +cloneGraph(node1); + + + diff --git a/longest-repeating-character-replacement/Blossssom.ts b/longest-repeating-character-replacement/Blossssom.ts new file mode 100644 index 0000000000..f6cf01ceab --- /dev/null +++ b/longest-repeating-character-replacement/Blossssom.ts @@ -0,0 +1,39 @@ +/** + * @param s - 대문자 알파벳 문자열 + * @param k - 문자열을 바꿀 수 있는 횟수 + * @returns - 동일 문자를 포함한 가장 긴 문자열의 길이 + */ +function characterReplacement(s: string, k: number): number { + const countMap = new Map(); + + let left = 0; + let maxRepeatCount = 0; + let maxLength = 0; + + for (let right = 0; right < s.length; right++) { + const char = s[right]; + + // 문자 카운트 누적 + countMap.set(char, (countMap.get(char) || 0) + 1); + + maxRepeatCount = Math.max(maxRepeatCount, countMap.get(char)!); + + // (현재 윈도우 길이 - 최대 빈도수) 가 k 보다 크면 윈도우 축소 => left 증가 + while (right - left + 1 - maxRepeatCount > k) { + const leftChar = s[left]; + countMap.set(leftChar, countMap.get(leftChar)! - 1); + left++; + } + + // 유효한 윈도우 중 최대 길이 + maxLength = Math.max(maxLength, right - left + 1); + } + + return maxLength; +} + +const s = "ABAB"; +const k = 2; +characterReplacement(s, k); + + diff --git a/palindromic-substrings/Blossssom.ts b/palindromic-substrings/Blossssom.ts new file mode 100644 index 0000000000..11e4eb0d83 --- /dev/null +++ b/palindromic-substrings/Blossssom.ts @@ -0,0 +1,27 @@ +/** + * @param s - 문자열 + * @returns - 회문 (뒤집어 읽어도 같은 문자열) 의 갯수 반환 + * @description + * - 같은 글자여도 각각은 따로 판단하며 한 글자도 회문으로 판단 + */ + +function countSubstrings(s: string): number { + let n = s.length; + let cnt = 0; + const dp: boolean[][] = Array.from({ length: n }, () => Array(n).fill(false)); + + for (let j = 0; j < s.length; j++) { + for (let i = 0; i <= j; i++) { + if (s[i] === s[j] && (j - i <= 2 || dp[i + 1][j - 1])) { + dp[i][j] = true; + cnt++; + } + } + } + return cnt; +} + +const s = "aaa"; +countSubstrings(s); + + diff --git a/reverse-bits/Blossssom.ts b/reverse-bits/Blossssom.ts new file mode 100644 index 0000000000..60df8671ed --- /dev/null +++ b/reverse-bits/Blossssom.ts @@ -0,0 +1,30 @@ +/** + * @param n - 32비트 정수 부호 + * @returns - 비트를 뒤집은 정수 부호 + * @description + * - 풀이 1. 단순하게 변환, 0 채우기, 뒤집기 후 10진수로 재변환 + */ +// function reverseBits(n: number): number { +// const bits = n.toString(2).padStart(32, "0").split("").reverse().join(""); + +// return parseInt(bits, 2); +// } + +function reverseBits(n: number): number { + let result = 0; + for (let i = 0; i < 32; i++) { + // result를 왼쪽으로 1칸 밀어 공간생성, n의 가장 마지막 비트가 1인지 판단해 result에 할당 + result = (result << 1) | (n & 1); + + // 판단이 끝난 n의 마지막 비트를 밀어내며 다음 비트 준비 + n >>>= 1; + } + + // 32 비트로 전환해 return + return result >>> 0; +} + +const n = 43261596; +reverseBits(n); + +