diff --git a/binary-tree-level-order-traversal/hyer0705.ts b/binary-tree-level-order-traversal/hyer0705.ts new file mode 100644 index 000000000..a08e4d50f --- /dev/null +++ b/binary-tree-level-order-traversal/hyer0705.ts @@ -0,0 +1,71 @@ +/** + * 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) + * } + * } + */ + +// ----- [Solution 1] +// Time Complexity: O(n) +// Space Complexity: O(n) +function levelOrder(root: TreeNode | null): number[][] { + if (!root) return []; + + const answer: number[][] = []; + + const queue: [TreeNode, number][] = []; + let pointer = 0; + + queue.push([root, 0]); + + while (pointer < queue.length) { + const [currNode, level] = queue[pointer++]; + + if (!answer[level]) answer[level] = []; + + answer[level].push(currNode.val); + + if (currNode.left) queue.push([currNode.left, level + 1]); + if (currNode.right) queue.push([currNode.right, level + 1]); + } + + return answer; +} + +// ----- [Solution 2] +// Time Complexity: O(n) +// Space Complexity: O(n) +function levelOrder(root: TreeNode | null): number[][] { + if (!root) return []; + + const answer: number[][] = []; + + const queue: TreeNode[] = []; + queue.push(root); + + let pointer = 0; + + while (pointer < queue.length) { + const levelSize = queue.length - pointer; + const currentLevel: number[] = []; + + for (let i = 0; i < levelSize; i++) { + const currNode = queue[pointer++]; + currentLevel.push(currNode.val); + + if (currNode.left) queue.push(currNode.left); + if (currNode.right) queue.push(currNode.right); + } + + answer.push(currentLevel); + } + + return answer; +} diff --git a/counting-bits/hyer0705.ts b/counting-bits/hyer0705.ts new file mode 100644 index 000000000..c6a1089b9 --- /dev/null +++ b/counting-bits/hyer0705.ts @@ -0,0 +1,11 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) +function countBits(n: number): number[] { + const ans: number[] = Array.from({ length: n + 1 }, () => 0); + + for (let i = 1; i <= n; i++) { + ans[i] = ans[i >> 1] + (i & 1); + } + + return ans; +} diff --git a/house-robber-ii/hyer0705.ts b/house-robber-ii/hyer0705.ts new file mode 100644 index 000000000..e77a32949 --- /dev/null +++ b/house-robber-ii/hyer0705.ts @@ -0,0 +1,26 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) +function rob(nums: number[]): number { + const n = nums.length; + + if (n === 1) return nums[0]; + if (n === 2) return Math.max(nums[0], nums[1]); + + const getLinearRob = (start: number, end: number) => { + let prev2 = 0; + let prev1 = 0; + + for (let i = start; i <= end; i++) { + const current = Math.max(prev1, prev2 + nums[i]); + prev2 = prev1; + prev1 = current; + } + + return prev1; + }; + + let case1 = getLinearRob(0, n - 2); + let case2 = getLinearRob(1, n - 1); + + return Math.max(case1, case2); +} diff --git a/meeting-rooms-ii/hyer0705.ts b/meeting-rooms-ii/hyer0705.ts new file mode 100644 index 000000000..2f72f070c --- /dev/null +++ b/meeting-rooms-ii/hyer0705.ts @@ -0,0 +1,46 @@ +import { Interval } from "../home/lib/index"; +/** + * Definition of Interval: + * export class Interval { + * start :number; + * end :number; + * constructor(start :number, end :number) { + * this.start = start; + * this.end = end; + * } + * } + */ +import { MinHeap } from "@datastructures-js/heap"; + +export class Solution { + /** + * @param intervals: an array of meeting time intervals + * @return: the minimum number of conference rooms required + */ + // Time Complexity: O(n log n) + // Space Complexity: O(n) + minMeetingRooms(intervals: Interval[]): number { + intervals.sort((a, b) => a.start - b.start); + + const minHeap = new MinHeap(); + const n = intervals.length; + + let maximumRooms = 0; + for (let i = 0; i < n; i++) { + const { start, end } = intervals[i]; + + if (minHeap.isEmpty()) { + minHeap.insert(end); + maximumRooms = Math.max(maximumRooms, minHeap.size()); + continue; + } + + if (start >= minHeap.root()) { + minHeap.pop(); + } + minHeap.insert(end); + maximumRooms = Math.max(maximumRooms, minHeap.size()); + } + return maximumRooms; + } +} diff --git a/word-search-ii/hyer0705.ts b/word-search-ii/hyer0705.ts new file mode 100644 index 000000000..a1c5a6126 --- /dev/null +++ b/word-search-ii/hyer0705.ts @@ -0,0 +1,176 @@ +// 84ms +class TrieNode { + children: Map; + isEndOf: boolean; + word: string | null; + + constructor() { + this.children = new Map(); + this.word = null; + } +} +class Trie { + root: TrieNode; + + constructor() { + this.root = new TrieNode(); + } + + insert(word: string) { + let current = this.root; + + for (const char of word) { + if (!current.children.has(char)) { + current.children.set(char, new TrieNode()); + } + current = current.children.get(char); + } + + current.word = word; + } +} + +function findWords(board: string[][], words: string[]): string[] { + const m = board.length; + const n = board[0].length; + + const dictionary = new Trie(); + for (const word of words) { + dictionary.insert(word); + } + + const results: string[] = []; + + const dfs = (row: number, col: number, trieNode: TrieNode) => { + if (trieNode.word !== null) { + results.push(trieNode.word); + trieNode.word = null; + } + + const directions = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], + ]; + + for (const [dr, dc] of directions) { + const [nr, nc] = [row + dr, col + dc]; + + if (nr >= 0 && nr < m && nc >= 0 && nc < n) { + if (trieNode.children.has(board[nr][nc])) { + const char = board[nr][nc]; + const childNode = trieNode.children.get(char); + + if (board[nr][nc] !== "#") { + board[nr][nc] = "#"; + dfs(nr, nc, trieNode.children.get(char)); + board[nr][nc] = char; + } + + if (childNode.children.size === 0 && childNode.word === null) { + trieNode.children.delete(char); + } + } + } + } + }; + + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + const firstChar = board[i][j]; + if (dictionary.root.children.has(firstChar)) { + board[i][j] = "#"; + dfs(i, j, dictionary.root.children.get(firstChar)); + board[i][j] = firstChar; + } + } + } + + return results; +} + +// 2312ms +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) { + let current = this.root; + + for (const char of word) { + if (!current.children.has(char)) { + current.children.set(char, new TrieNode()); + } + current = current.children.get(char)!; + } + + current.word = word; + } +} + +function findWords(board: string[][], words: string[]): string[] { + const m = board.length; + const n = board[0].length; + + const dictionary = new Trie(); + for (const word of words) { + dictionary.insert(word); + } + + const results: string[] = []; + + const dfs = (row: number, col: number, trieNode: TrieNode, path: string) => { + if (trieNode.word === path) { + results.push(path); + trieNode.word = null; + } + + const directions = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], + ]; + + for (const [dr, dc] of directions) { + const [nr, nc] = [row + dr, col + dc]; + + if (nr >= 0 && nr < m && nc >= 0 && nc < n) { + if (trieNode.children.has(board[nr][nc])) { + if (!isVisited.has(`${nr},${nc}`)) { + isVisited.add(`${nr},${nc}`); + dfs(nr, nc, trieNode.children.get(board[nr][nc])!, path + board[nr][nc]); + isVisited.delete(`${nr},${nc}`); + } + } + } + } + }; + + const isVisited = new Set(); // 'r,c' + for (let i = 0; i < m; i++) { + for (let j = 0; j < n; j++) { + const firstChar = board[i][j]; + if (dictionary.root.children.has(firstChar)) { + isVisited.add(`${i},${j}`); + dfs(i, j, dictionary.root.children.get(firstChar)!, firstChar); + isVisited.delete(`${i},${j}`); + } + } + } + + return results; +}