Skip to content

Commit 2d8c098

Browse files
authored
Merge pull request #1629 from Jeehay28/main
[Jeehay28] WEEK 14 Solutions
2 parents bf1c3a8 + 6529ad5 commit 2d8c098

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class TreeNode {
2+
val: number
3+
left: TreeNode | null
4+
right: TreeNode | null
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = (val===undefined ? 0 : val)
7+
this.left = (left===undefined ? null : left)
8+
this.right = (right===undefined ? null : right)
9+
}
10+
}
11+
12+
13+
// TC: O(n)
14+
// SC: O(n)
15+
function levelOrder(root: TreeNode | null): number[][] {
16+
if (!root) return [];
17+
18+
const result: number[][] = [];
19+
const queue: TreeNode[] = [root];
20+
21+
while (queue.length > 0) {
22+
const values: number[] = [];
23+
const level = queue.length;
24+
25+
for (let i = 0; i < level; i++) {
26+
const node = queue.shift()!;
27+
values.push(node.val);
28+
29+
if (node.left) {
30+
queue.push(node.left);
31+
}
32+
33+
if (node.right) {
34+
queue.push(node.right);
35+
}
36+
}
37+
38+
result.push(values);
39+
}
40+
41+
return result;
42+
}

counting-bits/Jeehay28.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// TC: O(n)
2+
// SC: O(n)
3+
function countBits(n: number): number[] {
4+
const dp: number[] = [];
5+
dp[0] = 0;
6+
7+
for (let i = 0; i <= n; i++) {
8+
dp[i] = dp[i >> 1] + (i & 1);
9+
}
10+
11+
// The number of 1s in the quotient (i >> 1) + number of 1s in the remainder (i & 1)
12+
// dp[i >> 1]: number of 1's in Math.floor(i / 2)
13+
// i & 1: 1 if i is odd, 0 if even
14+
15+
return dp;
16+
}
17+

house-robber-ii/Jeehay28.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
function rob(nums: number[]): number {
4+
if (nums.length === 1) return nums[0];
5+
6+
const robHouse = (start: number, end: number) => {
7+
let prevSum = 0;
8+
let prevPrevSum = 0;
9+
10+
for (let i = start; i <= end; i++) {
11+
const temp = Math.max(prevSum, prevPrevSum + nums[i]);
12+
prevPrevSum = prevSum;
13+
prevSum = temp;
14+
}
15+
16+
return prevSum;
17+
};
18+
19+
return Math.max(robHouse(0, nums.length - 2), robHouse(1, nums.length - 1));
20+
}

word-search-ii/Jeehay28.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
// TC: O(K × L + M × N × 4^L)
2+
// SC: O(K × L + L)
3+
// K: num of words
4+
// L: length of the longest word
5+
6+
function findWords(board: string[][], words: string[]): string[] {
7+
const result: string[] = [];
8+
const root: TrieNode = buildTrie(words);
9+
const rows = board.length;
10+
const cols = board[0].length;
11+
12+
function backtrack(row: number, col: number, node: TrieNode) {
13+
const char = board[row][col];
14+
const nextNode = node[char] as TrieNode;
15+
16+
if (!nextNode) return;
17+
18+
const word = nextNode["$"];
19+
if (typeof word === "string") {
20+
result.push(word);
21+
delete nextNode["$"]; // avoid duplicate
22+
}
23+
24+
board[row][col] = ""; // mark as visited
25+
26+
const directions = [
27+
[1, 0],
28+
[-1, 0],
29+
[0, 1],
30+
[0, -1],
31+
];
32+
33+
for (const [dx, dy] of directions) {
34+
const newRow = row + dx;
35+
const newCol = col + dy;
36+
37+
if (
38+
newRow >= 0 &&
39+
newRow < rows &&
40+
newCol >= 0 &&
41+
newCol < cols &&
42+
board[newRow][newCol] !== "$"
43+
) {
44+
backtrack(newRow, newCol, nextNode);
45+
}
46+
}
47+
48+
board[row][col] = char;
49+
}
50+
51+
for (let r = 0; r < rows; r++) {
52+
for (let c = 0; c < cols; c++) {
53+
backtrack(r, c, root);
54+
}
55+
}
56+
57+
return result;
58+
}
59+
60+
// Trie
61+
// {
62+
// "e": {
63+
// "a": {
64+
// "t": {
65+
// "#": "eat"
66+
// }
67+
// }
68+
// }
69+
// }
70+
71+
type TrieNode = {
72+
[key: string]: TrieNode | string;
73+
};
74+
75+
function buildTrie(words: string[]): TrieNode {
76+
const root: TrieNode = {};
77+
for (const word of words) {
78+
let node = root;
79+
for (const char of word) {
80+
if (!node[char]) node[char] = {};
81+
node = node[char] as TrieNode;
82+
}
83+
node["$"] = word; // end of word
84+
}
85+
return root;
86+
}

0 commit comments

Comments
 (0)