Skip to content

Commit f6d101c

Browse files
authored
Merge pull request #1094 from Jeehay28/main
[Jeehay28] WEEK 14
2 parents cc421d2 + 7e0d5a5 commit f6d101c

File tree

4 files changed

+227
-0
lines changed

4 files changed

+227
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// ✅ Time Complexity: O(n), where n is the number of nodes in the tree
2+
// ✅ Space Complexity: O(n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {TreeNode} root
14+
* @return {number[][]}
15+
*/
16+
var levelOrder = function (root) {
17+
if (!root) return [];
18+
19+
let output = [];
20+
let queue = [root];
21+
22+
while (queue.length > 0) {
23+
let values = [];
24+
25+
const lengthQueue = queue.length;
26+
27+
for (let i = 0; i < lengthQueue; i++) {
28+
const node = queue.shift();
29+
values.push(node.val);
30+
if (node.left) queue.push(node.left);
31+
if (node.right) queue.push(node.right);
32+
}
33+
output.push(values);
34+
}
35+
return output;
36+
};
37+

counting-bits/Jeehay28.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ✅ Time Complexity: O(n)
2+
// ✅ Space Complexity: O(n)
3+
4+
/**
5+
* @param {number} n
6+
* @return {number[]}
7+
*/
8+
var countBits = function (n) {
9+
// dp[i] = 1 + dp[i - MSB]
10+
// MSB(Most Significant Bit)
11+
12+
let dp = new Array(n + 1).fill(0);
13+
let msb = 1;
14+
15+
for (let i = 1; i <= n; i++) {
16+
if (msb * 2 === i) {
17+
msb = i;
18+
}
19+
20+
dp[i] = 1 + dp[i - msb];
21+
}
22+
23+
return dp;
24+
};
25+
26+
// ✅ Time Complexity: O(n * logn)
27+
// ✅ Space Complexity: O(n)
28+
29+
/**
30+
* @param {number} n
31+
* @return {number[]}
32+
*/
33+
// var countBits = function (n) {
34+
// let result = [0];
35+
// const count = (num) => {
36+
// let cnt = 0;
37+
// while (num !== 0) {
38+
// cnt += num % 2;
39+
// num = Math.floor(num / 2);
40+
// }
41+
// return cnt;
42+
// };
43+
44+
// for (let i = 1; i <= n; i++) {
45+
// const temp = count(i);
46+
// result.push(temp);
47+
// }
48+
49+
// return result;
50+
// };
51+

house-robber-ii/Jeehay28.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// ✅ Time Complexity: O(n)
2+
// ✅ Space Complexity: O(1)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number}
7+
*/
8+
var rob = function (nums) {
9+
// Edge case: If there's only one house, return its value
10+
if (nums.length === 1) return nums[0];
11+
12+
// helper function
13+
const robHouses = (start, end) => {
14+
// prev1: stores the maximum money robbed up to the previous house.
15+
// prev2: stores the maximum money robbed up to the house before the previous house.
16+
let prev1 = 0,
17+
prev2 = 0;
18+
19+
for (let i = start; i <= end; i++) {
20+
let temp = Math.max(prev1, prev2 + nums[i]);
21+
22+
prev2 = prev1;
23+
24+
prev1 = temp;
25+
}
26+
27+
return prev1;
28+
};
29+
30+
// Excluding the last house: robHouses(0, nums.length - 2)
31+
// Excluding the first house: robHouses(1, nums.length - 1)
32+
33+
return Math.max(robHouses(0, nums.length - 2), robHouses(1, nums.length - 1));
34+
};
35+

word-search-ii/Jeehay28.js

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// const words = ["oath", "pea", "eat", "rain"];
2+
// const trie = buildTrie(words);
3+
4+
// Trie in JavaScript Object Notation:
5+
// {
6+
// "o": { "a": { "t": { "h": { "word": "oath" } } } },
7+
// "p": { "e": { "a": { "word": "pea" } } },
8+
// "e": { "a": { "t": { "word": "eat" } } },
9+
// "r": { "a": { "i": { "n": { "word": "rain" } } } }
10+
// }
11+
12+
class TrieNode {
13+
constructor() {
14+
this.children = {}; // Stores child nodes (next characters)
15+
this.word = null; // Stores the word when a full word is formed
16+
}
17+
}
18+
19+
const buildTrie = (words) => {
20+
let root = new TrieNode(); // Create the root node
21+
22+
for (const word of words) {
23+
// Iterate over each word
24+
let node = root;
25+
26+
for (const char of word) {
27+
// Iterate over each character in the word
28+
if (!node.children[char]) {
29+
node.children[char] = new TrieNode(); // Create node if missing
30+
}
31+
node = node.children[char]; // Move to the next node
32+
}
33+
34+
node.word = word; // Store word at the end node
35+
}
36+
37+
return root;
38+
};
39+
40+
// ✅ Time Complexity: O(N * L + M * 4^L)
41+
// ✅ Space Complexity: O(N * L + M)
42+
43+
// N represents the number of words in the words array.
44+
// L represents the maximum length of a word in the words array.
45+
// M represents the total number of cells on the board, which is the product of the number of rows and columns: M = rows × cols
46+
47+
/**
48+
* @param {character[][]} board
49+
* @param {string[]} words
50+
* @return {string[]}
51+
*/
52+
var findWords = function (board, words) {
53+
const root = buildTrie(words);
54+
const result = new Set(); // To store found words
55+
const rows = board.length,
56+
cols = board[0].length;
57+
58+
const dfs = (node, r, c) => {
59+
if (
60+
r < 0 ||
61+
c < 0 ||
62+
r >= rows ||
63+
c >= cols ||
64+
!node.children[board[r][c]]
65+
) {
66+
return;
67+
}
68+
69+
const char = board[r][c];
70+
node = node.children[char]; // Move to the next Trie node
71+
72+
if (node.word) {
73+
// If a word is found at this node
74+
result.add(node.word); // Add it to the result set
75+
node.word = null; // Avoid duplicate results
76+
}
77+
78+
board[r][c] = "#"; // Temporarily mark visited cell
79+
80+
// Explore all 4 directions
81+
dfs(node, r + 1, c);
82+
dfs(node, r - 1, c);
83+
dfs(node, r, c + 1);
84+
dfs(node, r, c - 1);
85+
86+
board[r][c] = char; // Restore the original character
87+
88+
// 🔥 Remove node if it has no children (prune Trie)
89+
if (Object.keys(node.children).length === 0) {
90+
delete node.children[char];
91+
}
92+
};
93+
94+
for (let r = 0; r < rows; r++) {
95+
for (let c = 0; c < cols; c++) {
96+
if (root.children[board[r][c]]) {
97+
dfs(root, r, c);
98+
}
99+
}
100+
}
101+
102+
return Array.from(result);
103+
};
104+

0 commit comments

Comments
 (0)