Skip to content

[Evan] Week 3 #73

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 5 commits into from
May 18, 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
30 changes: 30 additions & 0 deletions climbing-stairs/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function (n) {
if (n <= 2) {
return n;
}

let [firstStair, secondStair] = [1, 2];
let thirdStair;

for (let i = 3; i <= n; i++) {
thirdStair = firstStair + secondStair;

[firstStair, secondStair] = [secondStair, thirdStair];
}

return thirdStair;
};

// Time Complexity: O(n)
// Reason: The function uses a loop that iterates from 3 to n,
// which means it runs in linear time with respect to n.

// Space Complexity: O(1)
// Reason: The function uses a fixed amount of extra space
// (a few integer variables: first, second, and third).
// It does not use any additional data structures
// that grow with the input size n.
39 changes: 39 additions & 0 deletions maximum-depth-of-binary-tree/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function (root) {
if (root === null) {
return 0;
}

const leftDepth = maxDepth(root.left);
const rightDepth = maxDepth(root.right);

return Math.max(leftDepth, rightDepth) + 1;
};

/**
* Time Complexity: O(n), where n is the number of nodes in the binary tree.
* Reason:
* the function visits each node exactly once in order to compute the depth of the tree,
* which ensures that each node is processed a single time.
*
* Space Complexity: O(h), where h is the height of the binary tree.
* Reason:
* The recursion stack used during the depth-first traversal.
*
* In the worst case, where the tree is completely unbalanced,
* the height h can be equal to the number of nodes n, leading to a space complexity of O(n).
*
* In the best case, where the tree is balanced, the height h is log(n),
* leading to a space complexity of O(log(n)).
*/
40 changes: 40 additions & 0 deletions meeting-rooms/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Definition of Interval:
* class Interval {
* constructor(start, end) {
* this.start = start;
* this.end = end;
* }
* }
*/

export class Solution {
/**
* @param intervals: an array of meeting time intervals
* @return: if a person could attend all meetings
*/
canAttendMeetings(intervals) {
intervals.sort((a, b) => a[0] - b[0]);

for (let i = 1; i < intervals.length; i++) {
const startTime = intervals[i][0];
const previousMeetingEndTime = intervals[i - 1][1];

if (previousMeetingEndTime > startTime) {
return false;
}
}

return true;
}
}

/**
* Time complexity: O(nlogn), where n is the number of meetings
* Reason:
* We sort the meetings by start time, which takes O(nlogn) time.
* Then we iterate through the meetings once, which takes O(n) time.
*
* Space complexity: O(1)
* Reason: We use a constant amount of extra space.
*/
26 changes: 26 additions & 0 deletions same-tree/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (!p && !q) {
return true;
}

if (!p || !q || p.val !== q.val) {
return false;
}

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};

/**
* Time Complexity: O(n)
* Reason: We visit each node exactly once.
*
* Space Complexity: O(n)
* Reason:
* The space used by the call stack is proportional
* to the height of the tree.
*/
63 changes: 63 additions & 0 deletions subtree-of-another-tree/evan.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function TreeNode(val, left, right) {
this.val = val === undefined ? 0 : val;
this.left = left === undefined ? null : left;
this.right = right === undefined ? null : right;
}

/**
* @param {TreeNode} p
* @param {TreeNode} q
* @return {boolean}
*/
var isSameTree = function (p, q) {
if (!p && !q) {
return true;
}

if (!p || !q || p.val !== q.val) {
return false;
}

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
};

/**
* @param {TreeNode} root
* @param {TreeNode} subRoot
* @return {boolean}
*/
var isSubtree = function (root, subRoot) {
if (subRoot === null) {
return true;
}

if (root === null && subRoot !== null) {
return false;
}

if (isSameTree(root, subRoot)) {
return true;
}

return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
};

/**
* Time Complexity: O(n * m), n: number of nodes in tree `subRoot`, m: number of nodes in tree `root`
* Reason:
* The `isSameTree` function is called at every node of tree `root`.
* The time complexity of the `isSameTree` function is O(n) since it compares all nodes of the two trees,
* where n is the number of nodes in tree `subRoot`.
*
* Since `isSameTree` is called at every node of tree `root`,
* the worst-case time complexity is O(n * m),
* where m is the number of nodes in tree `root`.
*/

/**
* Space Complexity: O(h), where h is the height of tree `root`
* Reason:
* The space complexity is determined by the depth of the recursion stack.
* In the worst case, the recursion will reach the maximum depth of tree `root`, which is its height h.
* Therefore, the space complexity is O(h).
*/