From 4af9c251c4ddb68310edbf7e326459296e23d58a Mon Sep 17 00:00:00 2001 From: HC-kang Date: Thu, 14 Nov 2024 08:12:34 +0900 Subject: [PATCH 1/5] Feat: 190. Reverse Bits --- reverse-bits/HC-kang.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 reverse-bits/HC-kang.ts diff --git a/reverse-bits/HC-kang.ts b/reverse-bits/HC-kang.ts new file mode 100644 index 000000000..e4d78c5cc --- /dev/null +++ b/reverse-bits/HC-kang.ts @@ -0,0 +1,13 @@ +/** + * https://leetcode.com/problems/reverse-bits + * T.C. O(1) + * S.C. O(1) + */ +function reverseBits(n: number): number { + let result = 0; + for (let i = 0; i < 32; i++) { + result = (result << 1) | (n & 1); + n >>= 1; + } + return result >>> 0; +} From bf1cca53a92e84e9e185ac6a9446e1922e0bb266 Mon Sep 17 00:00:00 2001 From: HC-kang Date: Sat, 16 Nov 2024 12:10:18 +0900 Subject: [PATCH 2/5] Feat: 102. Binary Tree Level Order Traversal --- binary-tree-level-order-traversal/HC-kang.ts | 57 ++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 binary-tree-level-order-traversal/HC-kang.ts diff --git a/binary-tree-level-order-traversal/HC-kang.ts b/binary-tree-level-order-traversal/HC-kang.ts new file mode 100644 index 000000000..3ed222c69 --- /dev/null +++ b/binary-tree-level-order-traversal/HC-kang.ts @@ -0,0 +1,57 @@ +// 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; +// } +// } + +/** + * https://leetcode.com/problems/binary-tree-level-order-traversal + * T.C. O(n) + * S.C. O(n) + */ +function levelOrder(root: TreeNode | null): number[][] { + const result: number[][] = []; + function dfs(node: TreeNode | null, level: number) { + if (!node) return; + if (!result[level]) result[level] = []; + result[level].push(node.val); + dfs(node.left, level + 1); + dfs(node.right, level + 1); + } + dfs(root, 0); + return result; +} + +/** + * bfs + * T.C. O(n) + * S.C. O(n) + */ +function levelOrder(root: TreeNode | null): number[][] { + const result: number[][] = []; + if (!root) return result; + + const queue: TreeNode[] = [root]; + let start = 0; + + while (queue[start]) { + const levelSize = queue.length - start; + const currentLevel: number[] = []; + + for (let i = 0; i < levelSize; i++) { + const node = queue[start++]; + currentLevel.push(node.val); + if (node.left) queue.push(node.left); + if (node.right) queue.push(node.right); + } + + result.push(currentLevel); + } + + return result; +} From 105851104163e8ab12031ac5b9b4413933f821ab Mon Sep 17 00:00:00 2001 From: HC-kang Date: Sat, 16 Nov 2024 12:10:30 +0900 Subject: [PATCH 3/5] Feat: 213. House Robber II --- house-robber-ii/HC-kang.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 house-robber-ii/HC-kang.ts diff --git a/house-robber-ii/HC-kang.ts b/house-robber-ii/HC-kang.ts new file mode 100644 index 000000000..63d9d6a6a --- /dev/null +++ b/house-robber-ii/HC-kang.ts @@ -0,0 +1,26 @@ +/** + * https://leetcode.com/problems/house-robber-ii + * T.C. O(n) + * S.C. O(1) + */ +function rob(nums: number[]): number { + if (nums.length === 1) return nums[0]; + if (nums.length === 2) return Math.max(nums[0], nums[1]); + if (nums.length === 3) return Math.max(nums[0], nums[1], nums[2]); + + function robHelper(nums: number[]): number { + let prev = 0; + let curr = 0; + for (let i = 0; i < nums.length; i++) { + const temp = curr; + curr = Math.max(prev + nums[i], curr); + prev = temp; + } + return curr; + } + + const robFirst = robHelper(nums.slice(0, nums.length - 1)); + const robLast = robHelper(nums.slice(1)); + + return Math.max(robFirst, robLast); +} From 7cfd93cb8036ad2e0502700f86cae11df74ee78d Mon Sep 17 00:00:00 2001 From: HC-kang Date: Sat, 16 Nov 2024 12:10:42 +0900 Subject: [PATCH 4/5] Feat: 253. Meeting Rooms II --- meeting-rooms-ii/HC-kang.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 meeting-rooms-ii/HC-kang.ts diff --git a/meeting-rooms-ii/HC-kang.ts b/meeting-rooms-ii/HC-kang.ts new file mode 100644 index 000000000..9069a3c60 --- /dev/null +++ b/meeting-rooms-ii/HC-kang.ts @@ -0,0 +1,21 @@ +/** + * https://leetcode.com/problems/meeting-rooms-ii + * T.C. O(nlogn) + * S.C. O(n) + */ +function minMeetingRooms(intervals: number[][]): number { + const starts = intervals.map((interval) => interval[0]).sort((a, b) => a - b); + const ends = intervals.map((interval) => interval[1]).sort((a, b) => a - b); + + let rooms = 0; + let endIdx = 0; + for (let i = 0; i < starts.length; i++) { + if (starts[i] < ends[endIdx]) { + rooms++; + } else { + endIdx++; + } + } + + return rooms; +} From c4d6e96b00b52d63a800127766eb5c48a2fb27bd Mon Sep 17 00:00:00 2001 From: HC-kang Date: Sat, 16 Nov 2024 12:10:53 +0900 Subject: [PATCH 5/5] Feat: 212. Word Search II --- word-search-ii/HC-kang.ts | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 word-search-ii/HC-kang.ts diff --git a/word-search-ii/HC-kang.ts b/word-search-ii/HC-kang.ts new file mode 100644 index 000000000..e818593c3 --- /dev/null +++ b/word-search-ii/HC-kang.ts @@ -0,0 +1,62 @@ +/** + * https://leetcode.com/problems/word-search-ii + * T.C. O(W * L + M * N * 4^L) W: number of words, L: mean length of words, M: rows, N: cols + * S.C. O(W * L) + */ +function findWords(board: string[][], words: string[]): string[] { + const root = new TrieNode(); + for (const word of words) { + let node = root; + for (const char of word) { + if (!node.children[char]) { + node.children[char] = new TrieNode(); + } + node = node.children[char]; + } + node.word = word; + node.isEnd = true; + } + + const result = new Set(); + const rows = board.length; + const cols = board[0].length; + + function dfs(row: number, col: number, node: TrieNode): void { + if (row < 0 || row >= rows) return; + if (col < 0 || col >= cols) return; + if (!board[row][col]) return; + if (!node.children[board[row][col]]) return; + + const char = board[row][col]; + const currNode = node.children[char]; + + if (currNode.isEnd) { + result.add(currNode.word); + } + + board[row][col] = '#'; + + dfs(row + 1, col, currNode); + dfs(row - 1, col, currNode); + dfs(row, col + 1, currNode); + dfs(row, col - 1, currNode); + + board[row][col] = char; + } + + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + dfs(i, j, root); + } + } + + return Array.from(result); +} + +class TrieNode { + constructor( + public children: Record = {}, + public word: string = '', + public isEnd: boolean = false + ) {} +}