From 6b478f38318dfd7bdeb710b71842bb6f7166dd3d Mon Sep 17 00:00:00 2001 From: sounmind Date: Mon, 13 May 2024 18:07:23 -0400 Subject: [PATCH 1/5] solve: same tree --- same-tree/evan.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 same-tree/evan.js 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. + */ From d7f01268a747907f4eafcf694207b3ebe5d051e6 Mon Sep 17 00:00:00 2001 From: sounmind Date: Wed, 15 May 2024 13:41:28 -0400 Subject: [PATCH 2/5] solve: maximum depth of binary tree --- maximum-depth-of-binary-tree/evan.js | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maximum-depth-of-binary-tree/evan.js 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)). + */ From e938454b782d192258bc40e29e5b0fb8143fabf2 Mon Sep 17 00:00:00 2001 From: sounmind Date: Wed, 15 May 2024 14:56:14 -0400 Subject: [PATCH 3/5] solve: subtree of another tree --- subtree-of-another-tree/evan.js | 63 +++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 subtree-of-another-tree/evan.js 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). + */ From d6e57fbfa95e77eeae7b4ffd5be6c3e507364637 Mon Sep 17 00:00:00 2001 From: sounmind Date: Thu, 16 May 2024 14:06:09 -0400 Subject: [PATCH 4/5] solve: climbing stairs --- climbing-stairs/evan.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 climbing-stairs/evan.js 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. From 0f6d2c0714ef94f6a0b03caca0f1e6d0d2219335 Mon Sep 17 00:00:00 2001 From: sounmind Date: Fri, 17 May 2024 22:28:34 -0400 Subject: [PATCH 5/5] solve: meeting rooms --- meeting-rooms/evan.js | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 meeting-rooms/evan.js 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. + */