From e5a39a659010053d9748c98607bc6ccf34f54c20 Mon Sep 17 00:00:00 2001 From: leokim0922 Date: Mon, 13 May 2024 15:53:58 -0700 Subject: [PATCH 1/2] [Leo] 3rd Week solutions --- climbing-stairs/Leo.py | 12 ++++++++++++ maximum-depth-of-binary-tree/Leo.py | 14 ++++++++++++++ meeting-rooms/Leo.py | 11 +++++++++++ same-tree/Leo.py | 14 ++++++++++++++ subtree-of-another-tree/Leo.py | 29 +++++++++++++++++++++++++++++ 5 files changed, 80 insertions(+) create mode 100644 climbing-stairs/Leo.py create mode 100644 maximum-depth-of-binary-tree/Leo.py create mode 100644 meeting-rooms/Leo.py create mode 100644 same-tree/Leo.py create mode 100644 subtree-of-another-tree/Leo.py diff --git a/climbing-stairs/Leo.py b/climbing-stairs/Leo.py new file mode 100644 index 000000000..c7b88bddf --- /dev/null +++ b/climbing-stairs/Leo.py @@ -0,0 +1,12 @@ +class Solution: + def climbStairs(self, n: int) -> int: + fast, slow = 1, 1 + + for i in range(n - 1): + tmp = fast + fast = fast + slow + slow = tmp + + return fast + + ## TC: O(n), SC(1) diff --git a/maximum-depth-of-binary-tree/Leo.py b/maximum-depth-of-binary-tree/Leo.py new file mode 100644 index 000000000..56b7bd5c6 --- /dev/null +++ b/maximum-depth-of-binary-tree/Leo.py @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + + return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1 + + ## TC: O(n), SC: O(n) or O(logn) if it is balanced diff --git a/meeting-rooms/Leo.py b/meeting-rooms/Leo.py new file mode 100644 index 000000000..c5609c36b --- /dev/null +++ b/meeting-rooms/Leo.py @@ -0,0 +1,11 @@ +class Solution: + def canAttendMeetings(self, intervals: List[List[int]]) -> bool: + intervals.sort() ## nlogn + + for i in range(len(intervals) - 1): + if intervals[i][1] > intervals[i+1][0]: + return False + + return True + + ## TC: n(nlogn), SC: O(1) diff --git a/same-tree/Leo.py b/same-tree/Leo.py new file mode 100644 index 000000000..1d9c3f269 --- /dev/null +++ b/same-tree/Leo.py @@ -0,0 +1,14 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: + if p and q: + return p.val == q.val and self.isSameTree(p.right, q.right) and self.isSameTree(p.left, q.left) + else: + return p == q + + ## TC: O(n), SC: O(n) or O(logn) if it is balanced diff --git a/subtree-of-another-tree/Leo.py b/subtree-of-another-tree/Leo.py new file mode 100644 index 000000000..76cd14efa --- /dev/null +++ b/subtree-of-another-tree/Leo.py @@ -0,0 +1,29 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: + def dfs(root, subroot): + if not root and not subroot: + return True + if not root or not subroot: + return False + if root.val != subroot.val: + return False + + return dfs(root.left, subroot.left) and dfs(root.right, subroot.right) + + def solve(root, subroot): + if not root: + return False + if dfs(root, subroot): + return True + return solve(root.left, subroot) or solve(root.right, subroot) + + return solve(root, subRoot) + + ## TC: O(mn), where m and n denote len(subroot) and len(root) + ## SC: O(m+n) From 806acb33ad0c8558d6047cc2b1259d97f8303f0b Mon Sep 17 00:00:00 2001 From: leokim0922 Date: Tue, 14 May 2024 13:23:43 -0700 Subject: [PATCH 2/2] [Leo] 3rd Week solutions --- climbing-stairs/Leo.py | 2 +- meeting-rooms/Leo.py | 2 +- same-tree/Leo.py | 2 +- subtree-of-another-tree/Leo.py | 19 ++++++++++--------- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/climbing-stairs/Leo.py b/climbing-stairs/Leo.py index c7b88bddf..5a268ee13 100644 --- a/climbing-stairs/Leo.py +++ b/climbing-stairs/Leo.py @@ -9,4 +9,4 @@ def climbStairs(self, n: int) -> int: return fast - ## TC: O(n), SC(1) + ## TC: O(n), SC:O(1) diff --git a/meeting-rooms/Leo.py b/meeting-rooms/Leo.py index c5609c36b..98524c491 100644 --- a/meeting-rooms/Leo.py +++ b/meeting-rooms/Leo.py @@ -8,4 +8,4 @@ def canAttendMeetings(self, intervals: List[List[int]]) -> bool: return True - ## TC: n(nlogn), SC: O(1) + ## TC: O(nlogn), SC: O(1) diff --git a/same-tree/Leo.py b/same-tree/Leo.py index 1d9c3f269..a3ba02ead 100644 --- a/same-tree/Leo.py +++ b/same-tree/Leo.py @@ -9,6 +9,6 @@ def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: if p and q: return p.val == q.val and self.isSameTree(p.right, q.right) and self.isSameTree(p.left, q.left) else: - return p == q + return p is None and q is None ## TC: O(n), SC: O(n) or O(logn) if it is balanced diff --git a/subtree-of-another-tree/Leo.py b/subtree-of-another-tree/Leo.py index 76cd14efa..b65394d0e 100644 --- a/subtree-of-another-tree/Leo.py +++ b/subtree-of-another-tree/Leo.py @@ -6,24 +6,25 @@ # self.right = right class Solution: def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool: - def dfs(root, subroot): - if not root and not subroot: + + def dfs(root, subRoot): + if not root and not subRoot: return True - if not root or not subroot: + if not root or not subRoot: return False - if root.val != subroot.val: + if root.val != subRoot.val: return False - return dfs(root.left, subroot.left) and dfs(root.right, subroot.right) + return dfs(root.left, subRoot.left) and dfs(root.right, subRoot.right) - def solve(root, subroot): + def solve(root): if not root: return False - if dfs(root, subroot): + if dfs(root, subRoot): return True - return solve(root.left, subroot) or solve(root.right, subroot) + return solve(root.left) or solve(root.right) - return solve(root, subRoot) + return solve(root) ## TC: O(mn), where m and n denote len(subroot) and len(root) ## SC: O(m+n)