diff --git a/combination-sum/nhistory.js b/combination-sum/nhistory.js new file mode 100644 index 000000000..2fd9dd1b5 --- /dev/null +++ b/combination-sum/nhistory.js @@ -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; +}; diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/nhistory.js b/construct-binary-tree-from-preorder-and-inorder-traversal/nhistory.js new file mode 100644 index 000000000..eb5bb6e84 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/nhistory.js @@ -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) diff --git a/implement-trie-prefix-tree/nhistory.js b/implement-trie-prefix-tree/nhistory.js new file mode 100644 index 000000000..ee1d8e708 --- /dev/null +++ b/implement-trie-prefix-tree/nhistory.js @@ -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) diff --git a/kth-smallest-element-in-a-bst/nhistory.js b/kth-smallest-element-in-a-bst/nhistory.js new file mode 100644 index 000000000..5c90ec77e --- /dev/null +++ b/kth-smallest-element-in-a-bst/nhistory.js @@ -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) diff --git a/word-search/nhistory.js b/word-search/nhistory.js new file mode 100644 index 000000000..5c83f284e --- /dev/null +++ b/word-search/nhistory.js @@ -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)