Skip to content

[Jeehay28] WEEK 14 Solutions #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions binary-tree-level-order-traversal/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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)
}
}


// TC: O(n)
// SC: O(n)
function levelOrder(root: TreeNode | null): number[][] {
if (!root) return [];

const result: number[][] = [];
const queue: TreeNode[] = [root];

while (queue.length > 0) {
const values: number[] = [];
const level = queue.length;

for (let i = 0; i < level; i++) {
const node = queue.shift()!;
values.push(node.val);

if (node.left) {
queue.push(node.left);
}

if (node.right) {
queue.push(node.right);
}
}

result.push(values);
}

return result;
}
17 changes: 17 additions & 0 deletions counting-bits/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TC: O(n)
// SC: O(n)
function countBits(n: number): number[] {
const dp: number[] = [];
dp[0] = 0;

for (let i = 0; i <= n; i++) {
dp[i] = dp[i >> 1] + (i & 1);
}

// The number of 1s in the quotient (i >> 1) + number of 1s in the remainder (i & 1)
// dp[i >> 1]: number of 1's in Math.floor(i / 2)
// i & 1: 1 if i is odd, 0 if even

return dp;
}

20 changes: 20 additions & 0 deletions house-robber-ii/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// TC: O(n)
// SC: O(1)
function rob(nums: number[]): number {
if (nums.length === 1) return nums[0];

const robHouse = (start: number, end: number) => {
let prevSum = 0;
let prevPrevSum = 0;

for (let i = start; i <= end; i++) {
const temp = Math.max(prevSum, prevPrevSum + nums[i]);
prevPrevSum = prevSum;
prevSum = temp;
}

return prevSum;
};

return Math.max(robHouse(0, nums.length - 2), robHouse(1, nums.length - 1));
}
86 changes: 86 additions & 0 deletions word-search-ii/Jeehay28.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// TC: O(K × L + M × N × 4^L)
// SC: O(K × L + L)
// K: num of words
// L: length of the longest word

function findWords(board: string[][], words: string[]): string[] {
const result: string[] = [];
const root: TrieNode = buildTrie(words);
const rows = board.length;
const cols = board[0].length;

function backtrack(row: number, col: number, node: TrieNode) {
const char = board[row][col];
const nextNode = node[char] as TrieNode;

if (!nextNode) return;

const word = nextNode["$"];
if (typeof word === "string") {
result.push(word);
delete nextNode["$"]; // avoid duplicate
}

board[row][col] = ""; // mark as visited

const directions = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
];

for (const [dx, dy] of directions) {
const newRow = row + dx;
const newCol = col + dy;

if (
newRow >= 0 &&
newRow < rows &&
newCol >= 0 &&
newCol < cols &&
board[newRow][newCol] !== "$"
) {
backtrack(newRow, newCol, nextNode);
}
}

board[row][col] = char;
}

for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
backtrack(r, c, root);
}
}

return result;
}

// Trie
// {
// "e": {
// "a": {
// "t": {
// "#": "eat"
// }
// }
// }
// }

type TrieNode = {
[key: string]: TrieNode | string;
};

function buildTrie(words: string[]): TrieNode {
const root: TrieNode = {};
for (const word of words) {
let node = root;
for (const char of word) {
if (!node[char]) node[char] = {};
node = node[char] as TrieNode;
}
node["$"] = word; // end of word
}
return root;
}