diff --git a/climbing-stairs/evan.js b/climbing-stairs/evan.js new file mode 100644 index 000000000..100275ff7 --- /dev/null +++ b/climbing-stairs/evan.js @@ -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. diff --git a/maximum-depth-of-binary-tree/evan.js b/maximum-depth-of-binary-tree/evan.js new file mode 100644 index 000000000..9453b9160 --- /dev/null +++ b/maximum-depth-of-binary-tree/evan.js @@ -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)). + */ diff --git a/meeting-rooms/evan.js b/meeting-rooms/evan.js new file mode 100644 index 000000000..6558610a6 --- /dev/null +++ b/meeting-rooms/evan.js @@ -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. + */ diff --git a/same-tree/evan.js b/same-tree/evan.js new file mode 100644 index 000000000..3a2d9ed2f --- /dev/null +++ b/same-tree/evan.js @@ -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. + */ diff --git a/subtree-of-another-tree/evan.js b/subtree-of-another-tree/evan.js new file mode 100644 index 000000000..7e4a6f6bc --- /dev/null +++ b/subtree-of-another-tree/evan.js @@ -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). + */