Skip to content

Commit ab551a4

Browse files
authored
Merge pull request DaleStudy#137 from nhistory/week8
[Sehwan] Week8 solutions with JavaScript
2 parents ecb74c9 + 8601acf commit ab551a4

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

combination-sum/nhistory.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var combinationSum = function (candidates, target) {
2+
let output = [];
3+
4+
// Helper function for depth-first search
5+
const dfs = (index, currentVal, arr) => {
6+
// If the remaining value is less than 0, no need to proceed further
7+
if (currentVal < 0) return;
8+
// If we have found a valid combination
9+
if (currentVal === 0) {
10+
output.push([...arr]);
11+
return;
12+
}
13+
14+
// Iterate over the candidates starting from the current index
15+
for (let i = index; i < candidates.length; i++) {
16+
arr.push(candidates[i]);
17+
dfs(i, currentVal - candidates[i], arr);
18+
arr.pop(); // backtrack
19+
}
20+
};
21+
22+
// Start DFS with the initial target and empty combination
23+
dfs(0, target, []);
24+
25+
return output;
26+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var buildTree = function (preorder, inorder) {
2+
// Edge case: if either preorder or inorder is empty, return null
3+
if (!preorder.length || !inorder.length) return null;
4+
5+
// The first element in preorder is the root of the tree
6+
// Find the index of the root in the inorder array
7+
const root = new TreeNode(preorder[0]);
8+
const mid = inorder.indexOf(root.val);
9+
10+
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
11+
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
12+
13+
return root;
14+
};
15+
16+
// TC: O(n^2)
17+
// SC: O(n)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var Trie = function () {
2+
this.root = {};
3+
};
4+
5+
/**
6+
* @param {string} word
7+
* @return {void}
8+
*/
9+
Trie.prototype.insert = function (word) {
10+
let node = this.root;
11+
for (let char of word) {
12+
if (!node[char]) {
13+
node[char] = {};
14+
}
15+
node = node[char];
16+
}
17+
node.isEndOfWord = true;
18+
};
19+
20+
/**
21+
* @param {string} word
22+
* @return {boolean}
23+
*/
24+
Trie.prototype.search = function (word) {
25+
let node = this.root;
26+
for (let char of word) {
27+
if (!node[char]) {
28+
return false;
29+
}
30+
node = node[char];
31+
}
32+
return node.isEndOfWord === true;
33+
};
34+
35+
/**
36+
* @param {string} prefix
37+
* @return {boolean}
38+
*/
39+
Trie.prototype.startsWith = function (prefix) {
40+
let node = this.root;
41+
for (let char of prefix) {
42+
if (!node[char]) {
43+
return false;
44+
}
45+
node = node[char];
46+
}
47+
return true;
48+
};
49+
50+
// TC: O(n)
51+
// SC: O(n)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var kthSmallest = function (root, k) {
2+
let arr = [];
3+
inOrder(root, arr);
4+
5+
for (let i = 0; i < arr.length; i++) {
6+
if (i === k - 1) return arr[i];
7+
}
8+
};
9+
10+
const inOrder = (root, arr) => {
11+
if (!root) return;
12+
13+
inOrder(root.left, arr);
14+
arr.push(root.val);
15+
inOrder(root.right, arr);
16+
};
17+
18+
// TC: O(n)
19+
// SC: O(n)

word-search/nhistory.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
var exist = function (board, word) {
2+
const dfs = (row, col, index) => {
3+
if (word.length === index) return true;
4+
if (
5+
row < 0 ||
6+
col < 0 ||
7+
row >= board.length ||
8+
col >= board[0].length ||
9+
board[row][col] !== word[index]
10+
)
11+
return false;
12+
13+
board[row][col] = "#";
14+
if (
15+
dfs(row + 1, col, index + 1) ||
16+
dfs(row, col + 1, index + 1) ||
17+
dfs(row - 1, col, index + 1) ||
18+
dfs(row, col - 1, index + 1)
19+
)
20+
return true;
21+
22+
board[row][col] = word[index];
23+
};
24+
25+
for (let row = 0; row < board.length; row++) {
26+
for (let col = 0; col < board[row].length; col++) {
27+
if (board[row][col] === word[0] && dfs(row, col, 0)) return true;
28+
}
29+
}
30+
31+
return false;
32+
};
33+
34+
// N: board column length / M: board row length / L: word length
35+
// TC: O(N*M*4^L)
36+
// SC: O(L)

0 commit comments

Comments
 (0)