Skip to content

Commit 14c0fa3

Browse files
authored
Merge pull request #73 from sounmind/main
[Evan] Week 3
2 parents 73de4a0 + 0f6d2c0 commit 14c0fa3

File tree

5 files changed

+198
-0
lines changed

5 files changed

+198
-0
lines changed

climbing-stairs/evan.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
var climbStairs = function (n) {
6+
if (n <= 2) {
7+
return n;
8+
}
9+
10+
let [firstStair, secondStair] = [1, 2];
11+
let thirdStair;
12+
13+
for (let i = 3; i <= n; i++) {
14+
thirdStair = firstStair + secondStair;
15+
16+
[firstStair, secondStair] = [secondStair, thirdStair];
17+
}
18+
19+
return thirdStair;
20+
};
21+
22+
// Time Complexity: O(n)
23+
// Reason: The function uses a loop that iterates from 3 to n,
24+
// which means it runs in linear time with respect to n.
25+
26+
// Space Complexity: O(1)
27+
// Reason: The function uses a fixed amount of extra space
28+
// (a few integer variables: first, second, and third).
29+
// It does not use any additional data structures
30+
// that grow with the input size n.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function (root) {
14+
if (root === null) {
15+
return 0;
16+
}
17+
18+
const leftDepth = maxDepth(root.left);
19+
const rightDepth = maxDepth(root.right);
20+
21+
return Math.max(leftDepth, rightDepth) + 1;
22+
};
23+
24+
/**
25+
* Time Complexity: O(n), where n is the number of nodes in the binary tree.
26+
* Reason:
27+
* the function visits each node exactly once in order to compute the depth of the tree,
28+
* which ensures that each node is processed a single time.
29+
*
30+
* Space Complexity: O(h), where h is the height of the binary tree.
31+
* Reason:
32+
* The recursion stack used during the depth-first traversal.
33+
*
34+
* In the worst case, where the tree is completely unbalanced,
35+
* the height h can be equal to the number of nodes n, leading to a space complexity of O(n).
36+
*
37+
* In the best case, where the tree is balanced, the height h is log(n),
38+
* leading to a space complexity of O(log(n)).
39+
*/

meeting-rooms/evan.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition of Interval:
3+
* class Interval {
4+
* constructor(start, end) {
5+
* this.start = start;
6+
* this.end = end;
7+
* }
8+
* }
9+
*/
10+
11+
export class Solution {
12+
/**
13+
* @param intervals: an array of meeting time intervals
14+
* @return: if a person could attend all meetings
15+
*/
16+
canAttendMeetings(intervals) {
17+
intervals.sort((a, b) => a[0] - b[0]);
18+
19+
for (let i = 1; i < intervals.length; i++) {
20+
const startTime = intervals[i][0];
21+
const previousMeetingEndTime = intervals[i - 1][1];
22+
23+
if (previousMeetingEndTime > startTime) {
24+
return false;
25+
}
26+
}
27+
28+
return true;
29+
}
30+
}
31+
32+
/**
33+
* Time complexity: O(nlogn), where n is the number of meetings
34+
* Reason:
35+
* We sort the meetings by start time, which takes O(nlogn) time.
36+
* Then we iterate through the meetings once, which takes O(n) time.
37+
*
38+
* Space complexity: O(1)
39+
* Reason: We use a constant amount of extra space.
40+
*/

same-tree/evan.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {TreeNode} p
3+
* @param {TreeNode} q
4+
* @return {boolean}
5+
*/
6+
var isSameTree = function (p, q) {
7+
if (!p && !q) {
8+
return true;
9+
}
10+
11+
if (!p || !q || p.val !== q.val) {
12+
return false;
13+
}
14+
15+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
16+
};
17+
18+
/**
19+
* Time Complexity: O(n)
20+
* Reason: We visit each node exactly once.
21+
*
22+
* Space Complexity: O(n)
23+
* Reason:
24+
* The space used by the call stack is proportional
25+
* to the height of the tree.
26+
*/

subtree-of-another-tree/evan.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
function TreeNode(val, left, right) {
2+
this.val = val === undefined ? 0 : val;
3+
this.left = left === undefined ? null : left;
4+
this.right = right === undefined ? null : right;
5+
}
6+
7+
/**
8+
* @param {TreeNode} p
9+
* @param {TreeNode} q
10+
* @return {boolean}
11+
*/
12+
var isSameTree = function (p, q) {
13+
if (!p && !q) {
14+
return true;
15+
}
16+
17+
if (!p || !q || p.val !== q.val) {
18+
return false;
19+
}
20+
21+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
22+
};
23+
24+
/**
25+
* @param {TreeNode} root
26+
* @param {TreeNode} subRoot
27+
* @return {boolean}
28+
*/
29+
var isSubtree = function (root, subRoot) {
30+
if (subRoot === null) {
31+
return true;
32+
}
33+
34+
if (root === null && subRoot !== null) {
35+
return false;
36+
}
37+
38+
if (isSameTree(root, subRoot)) {
39+
return true;
40+
}
41+
42+
return isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
43+
};
44+
45+
/**
46+
* Time Complexity: O(n * m), n: number of nodes in tree `subRoot`, m: number of nodes in tree `root`
47+
* Reason:
48+
* The `isSameTree` function is called at every node of tree `root`.
49+
* The time complexity of the `isSameTree` function is O(n) since it compares all nodes of the two trees,
50+
* where n is the number of nodes in tree `subRoot`.
51+
*
52+
* Since `isSameTree` is called at every node of tree `root`,
53+
* the worst-case time complexity is O(n * m),
54+
* where m is the number of nodes in tree `root`.
55+
*/
56+
57+
/**
58+
* Space Complexity: O(h), where h is the height of tree `root`
59+
* Reason:
60+
* The space complexity is determined by the depth of the recursion stack.
61+
* In the worst case, the recursion will reach the maximum depth of tree `root`, which is its height h.
62+
* Therefore, the space complexity is O(h).
63+
*/

0 commit comments

Comments
 (0)