Skip to content

Commit 6bd3ab1

Browse files
committed
Add week 8 solutions
1 parent ab551a4 commit 6bd3ab1

File tree

5 files changed

+214
-0
lines changed

5 files changed

+214
-0
lines changed

combination-sum/yolophg.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time Complexity: O(n * target * k)
2+
// Space Complexity: O(target)
3+
4+
var combinationSum = function (candidates, target) {
5+
// initialize dp array to store combinations
6+
const dp = Array(target + 1)
7+
.fill(null)
8+
.map(() => []);
9+
10+
// sort candidates to ensure uniqueness and optimize processing
11+
candidates.sort((a, b) => a - b);
12+
13+
// one way to make sum 0 (by choosing no elements)
14+
dp[0] = [[]];
15+
16+
// iterate through each candidate
17+
for (let num of candidates) {
18+
// update dp array for current candidate
19+
for (let i = num; i <= target; i++) {
20+
for (let combination of dp[i - num]) {
21+
dp[i].push([...combination, num]);
22+
}
23+
}
24+
}
25+
26+
return dp[target];
27+
};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var buildTree = function (preorder, inorder) {
5+
// build a map for quick lookup of index positions in inorder array
6+
// map to store the value -> index relationships
7+
let map = new Map();
8+
inorder.forEach((value, index) => map.set(value, index));
9+
10+
// recursive function to construct the binary tree
11+
function buildTreeHelper(preStart, preEnd, inStart, inEnd) {
12+
// if there are no elements to consider
13+
if (preStart > preEnd || inStart > inEnd) return null;
14+
15+
// the first element in preorder is the root of the current tree
16+
let rootValue = preorder[preStart];
17+
// create a new root node
18+
let root = { val: rootValue, left: null, right: null };
19+
20+
// find the root in the inorder array to split the tree
21+
let inRootIndex = map.get(rootValue);
22+
// number of nodes in the left subtree
23+
let numsLeft = inRootIndex - inStart;
24+
25+
// recursively build the left subtree
26+
root.left = buildTreeHelper(
27+
preStart + 1,
28+
preStart + numsLeft,
29+
inStart,
30+
inRootIndex - 1
31+
);
32+
33+
// recursively build the right subtree
34+
root.right = buildTreeHelper(
35+
preStart + numsLeft + 1,
36+
preEnd,
37+
inRootIndex + 1,
38+
inEnd
39+
);
40+
41+
return root;
42+
}
43+
44+
// initiate the recursive construction
45+
return buildTreeHelper(0, preorder.length - 1, 0, inorder.length - 1);
46+
};

implement-trie-prefix-tree/yolophg.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Insert Method : Time Complexity: O(m)/Space Complexity: O(m)
2+
// Search Method : Time Complexity: O(m)/Space Complexity: O(1)
3+
// Starts With Method : Time Complexity: O(p)/Space Complexity: O(1)
4+
5+
var Trie = function () {
6+
this.trie = {};
7+
};
8+
9+
// insert a word into the Trie
10+
Trie.prototype.insert = function (word) {
11+
let node = this.trie;
12+
for (let char of word) {
13+
if (!node[char]) {
14+
// create a new node if it doesn't exist
15+
node[char] = {};
16+
}
17+
// move to the next node
18+
node = node[char];
19+
}
20+
// mark the end of the word
21+
node.isEnd = true;
22+
};
23+
24+
// search for a word in the Trie
25+
Trie.prototype.search = function (word) {
26+
let node = this.trie;
27+
for (let char of word) {
28+
if (!node[char]) {
29+
// character not found
30+
return false;
31+
}
32+
// move to the next node
33+
node = node[char];
34+
}
35+
// Ccheck if it's the end of a valid word
36+
return node.isEnd === true;
37+
};
38+
39+
// check if there's any word in the Trie that starts with the given prefix
40+
Trie.prototype.startsWith = function (prefix) {
41+
let node = this.trie;
42+
for (let char of prefix) {
43+
if (!node[char]) {
44+
// prefix not found
45+
return false;
46+
}
47+
// move to the next node
48+
node = node[char];
49+
}
50+
// prefix found
51+
return true;
52+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
var kthSmallest = function (root, k) {
5+
let stack = [];
6+
let current = root;
7+
let count = 0;
8+
9+
while (stack.length > 0 || current !== null) {
10+
// go to the leftmost node
11+
while (current !== null) {
12+
stack.push(current);
13+
current = current.left;
14+
}
15+
16+
// pop the node from the stack
17+
current = stack.pop();
18+
count++;
19+
20+
// if reached the kth node
21+
if (count === k) {
22+
return current.val;
23+
}
24+
25+
// go to the right node
26+
current = current.right;
27+
}
28+
29+
// if k is out of the range of the number of in the tree
30+
return null;
31+
};

word-search/yolophg.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Time Complexity: O(rows×cols)
2+
// Space Complexity: O(rows×cols)
3+
4+
var exist = function (board, word) {
5+
const rows = board.length;
6+
const cols = board[0].length;
7+
8+
// initialize a queue for BFS, starting from each cell in the board
9+
const queue = [];
10+
11+
// perform BFS from each cell in the board
12+
for (let i = 0; i < rows; i++) {
13+
for (let j = 0; j < cols; j++) {
14+
if (board[i][j] === word[0]) {
15+
queue.push([[i, j], 0, [[i, j]]]);
16+
}
17+
}
18+
}
19+
20+
// directions for BFS: up, down, left, right
21+
const directions = [
22+
[-1, 0],
23+
[1, 0],
24+
[0, -1],
25+
[0, 1],
26+
];
27+
28+
while (queue.length > 0) {
29+
const [[row, col], index, path] = queue.shift();
30+
31+
// if the entire word is found, return true
32+
if (index === word.length - 1) {
33+
return true;
34+
}
35+
36+
// explore neighbors
37+
for (const [dx, dy] of directions) {
38+
const newRow = row + dx;
39+
const newCol = col + dy;
40+
41+
// check boundaries and character match
42+
if (
43+
newRow >= 0 &&
44+
newRow < rows &&
45+
newCol >= 0 &&
46+
newCol < cols &&
47+
board[newRow][newCol] === word[index + 1] &&
48+
!path.some(([r, c]) => r === newRow && c === newCol)
49+
) {
50+
// enqueue the next cell to explore
51+
queue.push([[newRow, newCol], index + 1, [...path, [newRow, newCol]]]);
52+
}
53+
}
54+
}
55+
56+
// if no path was found, return false
57+
return false;
58+
};

0 commit comments

Comments
 (0)