diff --git a/combination-sum/yolophg.js b/combination-sum/yolophg.js new file mode 100644 index 000000000..6ff2a0db9 --- /dev/null +++ b/combination-sum/yolophg.js @@ -0,0 +1,27 @@ +// Time Complexity: O(n * target * k) +// Space Complexity: O(target) + +var combinationSum = function (candidates, target) { + // initialize dp array to store combinations + const dp = Array(target + 1) + .fill(null) + .map(() => []); + + // sort candidates to ensure uniqueness and optimize processing + candidates.sort((a, b) => a - b); + + // one way to make sum 0 (by choosing no elements) + dp[0] = [[]]; + + // iterate through each candidate + for (let num of candidates) { + // update dp array for current candidate + for (let i = num; i <= target; i++) { + for (let combination of dp[i - num]) { + dp[i].push([...combination, num]); + } + } + } + + return dp[target]; +}; diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/yolophg.js b/construct-binary-tree-from-preorder-and-inorder-traversal/yolophg.js new file mode 100644 index 000000000..0712372b9 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/yolophg.js @@ -0,0 +1,46 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var buildTree = function (preorder, inorder) { + // build a map for quick lookup of index positions in inorder array + // map to store the value -> index relationships + let map = new Map(); + inorder.forEach((value, index) => map.set(value, index)); + + // recursive function to construct the binary tree + function buildTreeHelper(preStart, preEnd, inStart, inEnd) { + // if there are no elements to consider + if (preStart > preEnd || inStart > inEnd) return null; + + // the first element in preorder is the root of the current tree + let rootValue = preorder[preStart]; + // create a new root node + let root = { val: rootValue, left: null, right: null }; + + // find the root in the inorder array to split the tree + let inRootIndex = map.get(rootValue); + // number of nodes in the left subtree + let numsLeft = inRootIndex - inStart; + + // recursively build the left subtree + root.left = buildTreeHelper( + preStart + 1, + preStart + numsLeft, + inStart, + inRootIndex - 1 + ); + + // recursively build the right subtree + root.right = buildTreeHelper( + preStart + numsLeft + 1, + preEnd, + inRootIndex + 1, + inEnd + ); + + return root; + } + + // initiate the recursive construction + return buildTreeHelper(0, preorder.length - 1, 0, inorder.length - 1); +}; diff --git a/implement-trie-prefix-tree/yolophg.js b/implement-trie-prefix-tree/yolophg.js new file mode 100644 index 000000000..9bf0497a4 --- /dev/null +++ b/implement-trie-prefix-tree/yolophg.js @@ -0,0 +1,52 @@ +// Insert Method : Time Complexity: O(m)/Space Complexity: O(m) +// Search Method : Time Complexity: O(m)/Space Complexity: O(1) +// Starts With Method : Time Complexity: O(p)/Space Complexity: O(1) + +var Trie = function () { + this.trie = {}; +}; + +// insert a word into the Trie +Trie.prototype.insert = function (word) { + let node = this.trie; + for (let char of word) { + if (!node[char]) { + // create a new node if it doesn't exist + node[char] = {}; + } + // move to the next node + node = node[char]; + } + // mark the end of the word + node.isEnd = true; +}; + +// search for a word in the Trie +Trie.prototype.search = function (word) { + let node = this.trie; + for (let char of word) { + if (!node[char]) { + // character not found + return false; + } + // move to the next node + node = node[char]; + } + // Ccheck if it's the end of a valid word + return node.isEnd === true; +}; + +// check if there's any word in the Trie that starts with the given prefix +Trie.prototype.startsWith = function (prefix) { + let node = this.trie; + for (let char of prefix) { + if (!node[char]) { + // prefix not found + return false; + } + // move to the next node + node = node[char]; + } + // prefix found + return true; +}; diff --git a/kth-smallest-element-in-a-bst/yolophg.js b/kth-smallest-element-in-a-bst/yolophg.js new file mode 100644 index 000000000..2cbe451a4 --- /dev/null +++ b/kth-smallest-element-in-a-bst/yolophg.js @@ -0,0 +1,31 @@ +// Time Complexity: O(n) +// Space Complexity: O(n) + +var kthSmallest = function (root, k) { + let stack = []; + let current = root; + let count = 0; + + while (stack.length > 0 || current !== null) { + // go to the leftmost node + while (current !== null) { + stack.push(current); + current = current.left; + } + + // pop the node from the stack + current = stack.pop(); + count++; + + // if reached the kth node + if (count === k) { + return current.val; + } + + // go to the right node + current = current.right; + } + + // if k is out of the range of the number of in the tree + return null; +}; diff --git a/word-search/yolophg.js b/word-search/yolophg.js new file mode 100644 index 000000000..24e200891 --- /dev/null +++ b/word-search/yolophg.js @@ -0,0 +1,58 @@ +// Time Complexity: O(rows×cols) +// Space Complexity: O(rows×cols) + +var exist = function (board, word) { + const rows = board.length; + const cols = board[0].length; + + // initialize a queue for BFS, starting from each cell in the board + const queue = []; + + // perform BFS from each cell in the board + for (let i = 0; i < rows; i++) { + for (let j = 0; j < cols; j++) { + if (board[i][j] === word[0]) { + queue.push([[i, j], 0, [[i, j]]]); + } + } + } + + // directions for BFS: up, down, left, right + const directions = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], + ]; + + while (queue.length > 0) { + const [[row, col], index, path] = queue.shift(); + + // if the entire word is found, return true + if (index === word.length - 1) { + return true; + } + + // explore neighbors + for (const [dx, dy] of directions) { + const newRow = row + dx; + const newCol = col + dy; + + // check boundaries and character match + if ( + newRow >= 0 && + newRow < rows && + newCol >= 0 && + newCol < cols && + board[newRow][newCol] === word[index + 1] && + !path.some(([r, c]) => r === newRow && c === newCol) + ) { + // enqueue the next cell to explore + queue.push([[newRow, newCol], index + 1, [...path, [newRow, newCol]]]); + } + } + } + + // if no path was found, return false + return false; +};