From 15a9a1dc6c406421b79e30f60d9899a2236c54dd Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Sun, 12 May 2024 21:21:07 +0900 Subject: [PATCH 1/7] solved maximum depth of binary tree --- maximum-depth-of-binary-tree/samthekorean.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/maximum-depth-of-binary-tree/samthekorean.py b/maximum-depth-of-binary-tree/samthekorean.py index e69de29bb..c9fbfb73b 100644 --- a/maximum-depth-of-binary-tree/samthekorean.py +++ b/maximum-depth-of-binary-tree/samthekorean.py @@ -0,0 +1,8 @@ +# Time complexity : O(n) +# Space complexity : O(n) +class Solution: + def maxDepth(self, root: Optional[TreeNode]) -> int: + if not root: + return 0 + + return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right)) From 32deff5d7c793b48e4637b808aec173f9d9049bd Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Sun, 12 May 2024 21:22:42 +0900 Subject: [PATCH 2/7] solved same tree --- same-tree/samthekorean.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/same-tree/samthekorean.py b/same-tree/samthekorean.py index e69de29bb..077cef932 100644 --- a/same-tree/samthekorean.py +++ b/same-tree/samthekorean.py @@ -0,0 +1,17 @@ +# Time complexity : O(n) +# Space complexity : O(n) +class Solution: + def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: + if not p and not q: + return True + + if not p and q: + return False + + if p and not q: + return False + + if p.val != q.val: + return False + + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) From 532c978b7f22a86b410d5e347d8c011f9694ae34 Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Sun, 12 May 2024 21:23:14 +0900 Subject: [PATCH 3/7] solved subtree of another tree --- subtree-of-another-tree/samthekorean.py | 29 +++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/subtree-of-another-tree/samthekorean.py b/subtree-of-another-tree/samthekorean.py index e69de29bb..e4165417b 100644 --- a/subtree-of-another-tree/samthekorean.py +++ b/subtree-of-another-tree/samthekorean.py @@ -0,0 +1,29 @@ +# Time complexity : O(n*m) +# Space complexity : Space complexity: O(h) h is the height of the call stack during the recursive traversal. +class Solution: + def isSubtree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: + if not q: + return True + + if not p: + return False + + if self.isSameTree(p, q): + return True + + return self.isSubtree(p.left, q) or self.isSubtree(p.right, q) + + def isSameTree(self, p, q) -> bool: + if not p and not q: + return True + + if not p and q: + return False + + if p and not q: + return False + + if p.val != q.val: + return False + + return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right) From be85556b412ba14b9be2751f55b78728ee21df6f Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Sun, 12 May 2024 22:18:44 +0900 Subject: [PATCH 4/7] solved climbing stairs : --- climbing-stairs/samthekorean.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/climbing-stairs/samthekorean.py b/climbing-stairs/samthekorean.py index e69de29bb..5aa8c9b4e 100644 --- a/climbing-stairs/samthekorean.py +++ b/climbing-stairs/samthekorean.py @@ -0,0 +1,18 @@ +# Time complexity : O(n) +# Space complexity : O(n) +class Solution: + def climbStairs(self, n: int) -> int: + a = 1 + b = 2 + result = 0 + if n == 1: + return a + + if n == 2: + return b + + for i in range(3, n + 1): + result = a + b + a, b = b, result + + return result From 9e962d8b0bb66cf3f0e0cbded5f4c5e6d4f1830f Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Sun, 12 May 2024 23:42:03 +0900 Subject: [PATCH 5/7] solved meeting rooms --- meeting-rooms/samthekorean.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/meeting-rooms/samthekorean.py b/meeting-rooms/samthekorean.py index e69de29bb..0ad9d2dbc 100644 --- a/meeting-rooms/samthekorean.py +++ b/meeting-rooms/samthekorean.py @@ -0,0 +1,9 @@ +# Time complexity : O(nlog(n)) +# Space complexity : O(1) +class Solution: + def canAttendMeetings(self, intervals: List[List[int]]) -> bool: + intervals.sort() + for i in range(len(intervals) - 1): + if intervals[i][1] > intervals[i + 1][0]: + return False + return True From d1f3963c878078bc6ecbc8aeb59a5533b221e2fa Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Mon, 13 May 2024 16:29:51 +0900 Subject: [PATCH 6/7] changed space complexity --- climbing-stairs/samthekorean.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/climbing-stairs/samthekorean.py b/climbing-stairs/samthekorean.py index 5aa8c9b4e..eecbe2969 100644 --- a/climbing-stairs/samthekorean.py +++ b/climbing-stairs/samthekorean.py @@ -1,5 +1,5 @@ # Time complexity : O(n) -# Space complexity : O(n) +# Space complexity : O(1) class Solution: def climbStairs(self, n: int) -> int: a = 1 From bcf3f63fbb92a806d9fbc2e5db5cec05c4a1860b Mon Sep 17 00:00:00 2001 From: Sam2022moon <104721736+Sam2022moon@users.noreply.github.com> Date: Mon, 13 May 2024 16:40:16 +0900 Subject: [PATCH 7/7] added more explainations on the space complexity. --- subtree-of-another-tree/samthekorean.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/subtree-of-another-tree/samthekorean.py b/subtree-of-another-tree/samthekorean.py index e4165417b..89c507cb6 100644 --- a/subtree-of-another-tree/samthekorean.py +++ b/subtree-of-another-tree/samthekorean.py @@ -1,5 +1,5 @@ -# Time complexity : O(n*m) -# Space complexity : Space complexity: O(h) h is the height of the call stack during the recursive traversal. +# Time complexity: O(n*m) +# Space complexity: O(r + s) isSubtree() method is internally calling isSameTree() so the total depth of the stack is sum of isSubtree() call stacks and isSameTree()'s call stacks. class Solution: def isSubtree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: if not q: