-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
15a9a1d
solved maximum depth of binary tree
SamTheKorean 32deff5
solved same tree
SamTheKorean 532c978
solved subtree of another tree
SamTheKorean be85556
solved climbing stairs
SamTheKorean 9e962d8
solved meeting rooms
SamTheKorean d1f3963
changed space complexity
SamTheKorean bcf3f63
added more explainations on the space complexity.
SamTheKorean File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally 💯!