Skip to content

[Helena] Week 8 solutions #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 28, 2024
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
27 changes: 27 additions & 0 deletions combination-sum/yolophg.js
Original file line number Diff line number Diff line change
@@ -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];
};
Original file line number Diff line number Diff line change
@@ -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);
};
52 changes: 52 additions & 0 deletions implement-trie-prefix-tree/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
31 changes: 31 additions & 0 deletions kth-smallest-element-in-a-bst/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
58 changes: 58 additions & 0 deletions word-search/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};