diff --git a/binary-tree-level-order-traversal/hoyeongkwak.ts b/binary-tree-level-order-traversal/hoyeongkwak.ts new file mode 100644 index 000000000..c4b5fb4df --- /dev/null +++ b/binary-tree-level-order-traversal/hoyeongkwak.ts @@ -0,0 +1,35 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ +/* +Time Complexity: O(n) +Space Complexity: O(n) +*/ +function levelOrder(root: TreeNode | null): number[][] { + if (root == null) return [] + const result: number[][] = [] + let queue: TreeNode[] = [root] + while (queue.length > 0) { + const levelSize = queue.length + const currentLevel: number[] = [] + for(let i = 0; i< levelSize; i++) { + const node = queue.shift()! + currentLevel.push(node.val) + + if (node.left) queue.push(node.left) + if (node.right) queue.push(node.right) + } + result.push(currentLevel) + } + return result +} diff --git a/counting-bits/hoyeongkwak.ts b/counting-bits/hoyeongkwak.ts new file mode 100644 index 000000000..1ee8d2ffd --- /dev/null +++ b/counting-bits/hoyeongkwak.ts @@ -0,0 +1,11 @@ +/* +Time Complexity: O(n) +Space Complexity: O(n) +*/ +function countBits(n: number): number[] { + const result: number[] = new Array(n + 1).fill(0) + for(let i = 1; i <= n; i++) { + result[i] = result[i >> 1] + (i & 1) + } + return result +}; diff --git a/house-robber-ii/hoyeongkwak.ts b/house-robber-ii/hoyeongkwak.ts new file mode 100644 index 000000000..35b28e302 --- /dev/null +++ b/house-robber-ii/hoyeongkwak.ts @@ -0,0 +1,18 @@ +/* +Time Complexity: O(n) +Space Complexity: O(1) +*/ +function rob(nums: number[]): number { + if (nums.length === 1) return nums[0] + const dp = (start, end): number => { + let prev = 0 + let curr = 0 + for (let i = start; i < end; i++) { + const temp = curr + curr = Math.max(prev + nums[i], curr) + prev = temp + } + return curr + } + return Math.max(dp(0, nums.length - 1), dp(1, nums.length)) +}; diff --git a/word-search-ii/hoyeongkwak.ts b/word-search-ii/hoyeongkwak.ts new file mode 100644 index 000000000..93787a761 --- /dev/null +++ b/word-search-ii/hoyeongkwak.ts @@ -0,0 +1,83 @@ +/* +L : 평균 단어 길이 +Time Complexity: O(M * N * 4^L) +Space Complexity: O(∑L + M × N) +*/ +class TrieNode { + children: Map + word: string | null + + constructor() { + this.children = new Map() + this.word = null + } +} + +class Trie { + root: TrieNode + constructor() { + this.root = new TrieNode() + } + + insert(word: string): void { + let node = this.root + + for (const char of word) { + if (!node.children.has(char)) { + node.children.set(char, new TrieNode()) + } + node = node.children.get(char) + } + node.word = word + } +} + +function dfs(board: string[][], row: number, col: number, node: TrieNode, visited: boolean[][], result: string[]): void { + const rows = board.length + const cols = board[0].length + if (row < 0 || row >= board.length || + col < 0 || col >= board[0].length || + visited[row][col]) { + return + } + const char = board[row][col] + if (!node.children.has(char)) { + return + } + node = node.children.get(char)! + + if (node.word !== null) { + result.push(node.word) + node.word = null + } + const originalChar = board[row][col] + visited[row][col] = true + const directions = [[-1, 0], [1, 0], [0, -1], [0, 1]] + for (const [dr, dc] of directions) { + dfs(board, row + dr, col + dc, node, visited, result) + } + visited[row][col] = false + } + +function findWords(board: string[][], words: string[]): string[] { + if (!board || board.length === 0 || !board[0] || board[0].length === 0) { + return [] + } + const result: string[] = [] + const trie = new Trie() + + for (const word of words) { + trie.insert(word) + } + + const rows = board.length + const cols = board[0].length + const visited = Array(rows).fill(null).map(() => Array(cols).fill(false)) + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + dfs(board, i, j, trie.root, visited, result) + } + } + return result +};