Skip to content

Commit 1387991

Browse files
authored
Merge pull request #1951 from hyer0705/main
[hyer0705] WEEK 14 Solutions
2 parents 459ae30 + 8b68505 commit 1387991

File tree

5 files changed

+330
-0
lines changed

5 files changed

+330
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
// ----- [Solution 1]
16+
// Time Complexity: O(n)
17+
// Space Complexity: O(n)
18+
function levelOrder(root: TreeNode | null): number[][] {
19+
if (!root) return [];
20+
21+
const answer: number[][] = [];
22+
23+
const queue: [TreeNode, number][] = [];
24+
let pointer = 0;
25+
26+
queue.push([root, 0]);
27+
28+
while (pointer < queue.length) {
29+
const [currNode, level] = queue[pointer++];
30+
31+
if (!answer[level]) answer[level] = [];
32+
33+
answer[level].push(currNode.val);
34+
35+
if (currNode.left) queue.push([currNode.left, level + 1]);
36+
if (currNode.right) queue.push([currNode.right, level + 1]);
37+
}
38+
39+
return answer;
40+
}
41+
42+
// ----- [Solution 2]
43+
// Time Complexity: O(n)
44+
// Space Complexity: O(n)
45+
function levelOrder(root: TreeNode | null): number[][] {
46+
if (!root) return [];
47+
48+
const answer: number[][] = [];
49+
50+
const queue: TreeNode[] = [];
51+
queue.push(root);
52+
53+
let pointer = 0;
54+
55+
while (pointer < queue.length) {
56+
const levelSize = queue.length - pointer;
57+
const currentLevel: number[] = [];
58+
59+
for (let i = 0; i < levelSize; i++) {
60+
const currNode = queue[pointer++];
61+
currentLevel.push(currNode.val);
62+
63+
if (currNode.left) queue.push(currNode.left);
64+
if (currNode.right) queue.push(currNode.right);
65+
}
66+
67+
answer.push(currentLevel);
68+
}
69+
70+
return answer;
71+
}

counting-bits/hyer0705.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
function countBits(n: number): number[] {
4+
const ans: number[] = Array.from({ length: n + 1 }, () => 0);
5+
6+
for (let i = 1; i <= n; i++) {
7+
ans[i] = ans[i >> 1] + (i & 1);
8+
}
9+
10+
return ans;
11+
}

house-robber-ii/hyer0705.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
function rob(nums: number[]): number {
4+
const n = nums.length;
5+
6+
if (n === 1) return nums[0];
7+
if (n === 2) return Math.max(nums[0], nums[1]);
8+
9+
const getLinearRob = (start: number, end: number) => {
10+
let prev2 = 0;
11+
let prev1 = 0;
12+
13+
for (let i = start; i <= end; i++) {
14+
const current = Math.max(prev1, prev2 + nums[i]);
15+
prev2 = prev1;
16+
prev1 = current;
17+
}
18+
19+
return prev1;
20+
};
21+
22+
let case1 = getLinearRob(0, n - 2);
23+
let case2 = getLinearRob(1, n - 1);
24+
25+
return Math.max(case1, case2);
26+
}

meeting-rooms-ii/hyer0705.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Interval } from "../home/lib/index";
2+
/**
3+
* Definition of Interval:
4+
* export class Interval {
5+
* start :number;
6+
* end :number;
7+
* constructor(start :number, end :number) {
8+
* this.start = start;
9+
* this.end = end;
10+
* }
11+
* }
12+
*/
13+
import { MinHeap } from "@datastructures-js/heap";
14+
15+
export class Solution {
16+
/**
17+
* @param intervals: an array of meeting time intervals
18+
* @return: the minimum number of conference rooms required
19+
*/
20+
// Time Complexity: O(n log n)
21+
// Space Complexity: O(n)
22+
minMeetingRooms(intervals: Interval[]): number {
23+
intervals.sort((a, b) => a.start - b.start);
24+
25+
const minHeap = new MinHeap();
26+
const n = intervals.length;
27+
28+
let maximumRooms = 0;
29+
for (let i = 0; i < n; i++) {
30+
const { start, end } = intervals[i];
31+
32+
if (minHeap.isEmpty()) {
33+
minHeap.insert(end);
34+
maximumRooms = Math.max(maximumRooms, minHeap.size());
35+
continue;
36+
}
37+
38+
if (start >= minHeap.root()) {
39+
minHeap.pop();
40+
}
41+
minHeap.insert(end);
42+
maximumRooms = Math.max(maximumRooms, minHeap.size());
43+
}
44+
return maximumRooms;
45+
}
46+
}

word-search-ii/hyer0705.ts

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// 84ms
2+
class TrieNode {
3+
children: Map<string, TrieNode>;
4+
isEndOf: boolean;
5+
word: string | null;
6+
7+
constructor() {
8+
this.children = new Map();
9+
this.word = null;
10+
}
11+
}
12+
class Trie {
13+
root: TrieNode;
14+
15+
constructor() {
16+
this.root = new TrieNode();
17+
}
18+
19+
insert(word: string) {
20+
let current = this.root;
21+
22+
for (const char of word) {
23+
if (!current.children.has(char)) {
24+
current.children.set(char, new TrieNode());
25+
}
26+
current = current.children.get(char);
27+
}
28+
29+
current.word = word;
30+
}
31+
}
32+
33+
function findWords(board: string[][], words: string[]): string[] {
34+
const m = board.length;
35+
const n = board[0].length;
36+
37+
const dictionary = new Trie();
38+
for (const word of words) {
39+
dictionary.insert(word);
40+
}
41+
42+
const results: string[] = [];
43+
44+
const dfs = (row: number, col: number, trieNode: TrieNode) => {
45+
if (trieNode.word !== null) {
46+
results.push(trieNode.word);
47+
trieNode.word = null;
48+
}
49+
50+
const directions = [
51+
[-1, 0],
52+
[1, 0],
53+
[0, -1],
54+
[0, 1],
55+
];
56+
57+
for (const [dr, dc] of directions) {
58+
const [nr, nc] = [row + dr, col + dc];
59+
60+
if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
61+
if (trieNode.children.has(board[nr][nc])) {
62+
const char = board[nr][nc];
63+
const childNode = trieNode.children.get(char);
64+
65+
if (board[nr][nc] !== "#") {
66+
board[nr][nc] = "#";
67+
dfs(nr, nc, trieNode.children.get(char));
68+
board[nr][nc] = char;
69+
}
70+
71+
if (childNode.children.size === 0 && childNode.word === null) {
72+
trieNode.children.delete(char);
73+
}
74+
}
75+
}
76+
}
77+
};
78+
79+
for (let i = 0; i < m; i++) {
80+
for (let j = 0; j < n; j++) {
81+
const firstChar = board[i][j];
82+
if (dictionary.root.children.has(firstChar)) {
83+
board[i][j] = "#";
84+
dfs(i, j, dictionary.root.children.get(firstChar));
85+
board[i][j] = firstChar;
86+
}
87+
}
88+
}
89+
90+
return results;
91+
}
92+
93+
// 2312ms
94+
class TrieNode {
95+
children: Map<string, TrieNode>;
96+
word: string | null;
97+
98+
constructor() {
99+
this.children = new Map();
100+
this.word = null;
101+
}
102+
}
103+
class Trie {
104+
root: TrieNode;
105+
106+
constructor() {
107+
this.root = new TrieNode();
108+
}
109+
110+
insert(word: string) {
111+
let current = this.root;
112+
113+
for (const char of word) {
114+
if (!current.children.has(char)) {
115+
current.children.set(char, new TrieNode());
116+
}
117+
current = current.children.get(char)!;
118+
}
119+
120+
current.word = word;
121+
}
122+
}
123+
124+
function findWords(board: string[][], words: string[]): string[] {
125+
const m = board.length;
126+
const n = board[0].length;
127+
128+
const dictionary = new Trie();
129+
for (const word of words) {
130+
dictionary.insert(word);
131+
}
132+
133+
const results: string[] = [];
134+
135+
const dfs = (row: number, col: number, trieNode: TrieNode, path: string) => {
136+
if (trieNode.word === path) {
137+
results.push(path);
138+
trieNode.word = null;
139+
}
140+
141+
const directions = [
142+
[-1, 0],
143+
[1, 0],
144+
[0, -1],
145+
[0, 1],
146+
];
147+
148+
for (const [dr, dc] of directions) {
149+
const [nr, nc] = [row + dr, col + dc];
150+
151+
if (nr >= 0 && nr < m && nc >= 0 && nc < n) {
152+
if (trieNode.children.has(board[nr][nc])) {
153+
if (!isVisited.has(`${nr},${nc}`)) {
154+
isVisited.add(`${nr},${nc}`);
155+
dfs(nr, nc, trieNode.children.get(board[nr][nc])!, path + board[nr][nc]);
156+
isVisited.delete(`${nr},${nc}`);
157+
}
158+
}
159+
}
160+
}
161+
};
162+
163+
const isVisited = new Set<string>(); // 'r,c'
164+
for (let i = 0; i < m; i++) {
165+
for (let j = 0; j < n; j++) {
166+
const firstChar = board[i][j];
167+
if (dictionary.root.children.has(firstChar)) {
168+
isVisited.add(`${i},${j}`);
169+
dfs(i, j, dictionary.root.children.get(firstChar)!, firstChar);
170+
isVisited.delete(`${i},${j}`);
171+
}
172+
}
173+
}
174+
175+
return results;
176+
}

0 commit comments

Comments
 (0)