Skip to content
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
26 changes: 26 additions & 0 deletions combination-sum/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var combinationSum = function (candidates, target) {
let output = [];

// Helper function for depth-first search
const dfs = (index, currentVal, arr) => {
// If the remaining value is less than 0, no need to proceed further
if (currentVal < 0) return;
// If we have found a valid combination
if (currentVal === 0) {
output.push([...arr]);
return;
}

// Iterate over the candidates starting from the current index
for (let i = index; i < candidates.length; i++) {
arr.push(candidates[i]);
dfs(i, currentVal - candidates[i], arr);
arr.pop(); // backtrack
}
};

// Start DFS with the initial target and empty combination
dfs(0, target, []);

return output;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var buildTree = function (preorder, inorder) {
// Edge case: if either preorder or inorder is empty, return null
if (!preorder.length || !inorder.length) return null;

// The first element in preorder is the root of the tree
// Find the index of the root in the inorder array
const root = new TreeNode(preorder[0]);
const mid = inorder.indexOf(root.val);

root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));

return root;
};

// TC: O(n^2)
// SC: O(n)
51 changes: 51 additions & 0 deletions implement-trie-prefix-tree/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
var Trie = function () {
this.root = {};
};

/**
* @param {string} word
* @return {void}
*/
Trie.prototype.insert = function (word) {
let node = this.root;
for (let char of word) {
if (!node[char]) {
node[char] = {};
}
node = node[char];
}
node.isEndOfWord = true;
};

/**
* @param {string} word
* @return {boolean}
*/
Trie.prototype.search = function (word) {
let node = this.root;
for (let char of word) {
if (!node[char]) {
return false;
}
node = node[char];
}
return node.isEndOfWord === true;
};

/**
* @param {string} prefix
* @return {boolean}
*/
Trie.prototype.startsWith = function (prefix) {
let node = this.root;
for (let char of prefix) {
if (!node[char]) {
return false;
}
node = node[char];
}
return true;
};

// TC: O(n)
// SC: O(n)
19 changes: 19 additions & 0 deletions kth-smallest-element-in-a-bst/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var kthSmallest = function (root, k) {
let arr = [];
inOrder(root, arr);

for (let i = 0; i < arr.length; i++) {
if (i === k - 1) return arr[i];
}
};

const inOrder = (root, arr) => {
if (!root) return;

inOrder(root.left, arr);
arr.push(root.val);
inOrder(root.right, arr);
};

// TC: O(n)
// SC: O(n)
36 changes: 36 additions & 0 deletions word-search/nhistory.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var exist = function (board, word) {
const dfs = (row, col, index) => {
if (word.length === index) return true;
if (
row < 0 ||
col < 0 ||
row >= board.length ||
col >= board[0].length ||
board[row][col] !== word[index]
)
return false;

board[row][col] = "#";
if (
dfs(row + 1, col, index + 1) ||
dfs(row, col + 1, index + 1) ||
dfs(row - 1, col, index + 1) ||
dfs(row, col - 1, index + 1)
)
return true;

board[row][col] = word[index];
};

for (let row = 0; row < board.length; row++) {
for (let col = 0; col < board[row].length; col++) {
if (board[row][col] === word[0] && dfs(row, col, 0)) return true;
}
}

return false;
};

// N: board column length / M: board row length / L: word length
// TC: O(N*M*4^L)
// SC: O(L)