Skip to content

[Helena] Week 3 solutions #84

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
May 19, 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
22 changes: 22 additions & 0 deletions climbing-stairs/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Time complexity : O(n)
// Space complexity : O(n)

var climbStairs = function(n) {
// create a memoization object to store results.
const memo = {};

// recursive function to calculate the number of ways to climb n steps.
function climb(n) {
// if n is 1 or 2, there's only one way to climb or two ways.
if (n <= 2) return n;

// if the result for n is already computed, return it from memo.
if (memo[n]) return memo[n];

// otherwise, compute the result recursively.
memo[n] = climb(n - 1) + climb(n - 2);
return memo[n];
}

return climb(n);
};
34 changes: 34 additions & 0 deletions maximum-depth-of-binary-tree/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Time complexity : O(n)
// Space complexity : O(n)

var maxDepth = function (root) {
// if the root is null, the depth is 0.
if (root === null) {
return 0;
}

// initialize the stack with the root node and its depth, which is 1.
const stack = [{ node: root, depth: 1 }];
let maxDepth = 0;

// process the stack till it is empty.
while (stack.length > 0) {
// pop the top element from the stack.
const { node, depth } = stack.pop();

if (node !== null) {
// update the maximum depth.
maxDepth = Math.max(maxDepth, depth);

// push the left and right children with their respective depths.
if (node.left !== null) {
stack.push({ node: node.left, depth: depth + 1 });
}
if (node.right !== null) {
stack.push({ node: node.right, depth: depth + 1 });
}
}
}

return maxDepth;
};
18 changes: 18 additions & 0 deletions meeting-rooms/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Time Complexity: O(n log n)
// Space Complexity: O(1)

class Solution {
canAttendMeetings(intervals) {
intervals.sort((a, b) => a.start - b.start);

// check for overlaps.
for (let i = 1; i < intervals.length; i++) {
// check if there is an overlap between the current interval and the previous one.
if (intervals[i].start < intervals[i - 1].end) {
return false;
}
}

return true;
}
}
32 changes: 32 additions & 0 deletions same-tree/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Time complexity : O(n)
// Space complexity : O(n)

var isSameTree = function(p, q) {
// initialize two queues for BFS.
let queue1 = [p];
let queue2 = [q];

while (queue1.length > 0 && queue2.length > 0) {
let node1 = queue1.shift();
let node2 = queue2.shift();

// if both nodes are null, continue to the next pair of nodes.
if (node1 === null && node2 === null) {
continue;
}

// if one of the nodes is null or their values are different, return false.
if (node1 === null || node2 === null || node1.val !== node2.val) {
return false;
}

// enqueue the left and right children of both nodes.
queue1.push(node1.left);
queue1.push(node1.right);
queue2.push(node2.left);
queue2.push(node2.right);
}

// if both queues are empty, the trees are the same
return queue1.length === 0 && queue2.length === 0;
};
25 changes: 25 additions & 0 deletions subtree-of-another-tree/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Time complexity : O(m * n)
// Space complexity : O(h)

// fuunction to check if two trees are identical.
var isIdentical = function(root1, root2) {
// if they are both empty, return true.
if (!root1 && !root2) return true;
// if either one of them is null and the other is not, return false.
if (!root1 || !root2) return false;

// if the right subtrees are identical, return true only if all conditions are met.
return root1.val === root2.val &&
isIdentical(root1.left, root2.left) &&
isIdentical(root1.right, root2.right);
}

var isSubtree = function(root, subRoot) {
// if the root is empty, return false
if (!root) return false;
// if the subtree rooted at subRoot is identical, return true.
if (isIdentical(root, subRoot)) return true;

// if find the subtree in either the left or right subtree, return true. otherwise, return false.
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
};