Skip to content

[SAM] Week 3 solutions. #70

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions climbing-stairs/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Time complexity : O(n)
# Space complexity : O(1)
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
8 changes: 8 additions & 0 deletions maximum-depth-of-binary-tree/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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))
9 changes: 9 additions & 0 deletions meeting-rooms/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions same-tree/samthekorean.py
Original file line number Diff line number Diff line change
@@ -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)
29 changes: 29 additions & 0 deletions subtree-of-another-tree/samthekorean.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finally 💯!

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)