-
-
Notifications
You must be signed in to change notification settings - Fork 194
[Leo] 3rd Week solutions #74
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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:O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: O(nlogn), SC: O(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 is None and q is None | ||
|
||
## TC: O(n), SC: O(n) or O(logn) if it is balanced |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# 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): | ||
if not root: | ||
return False | ||
if dfs(root, subRoot): | ||
return True | ||
return solve(root.left) or solve(root.right) | ||
|
||
return solve(root) | ||
|
||
## TC: O(mn), where m and n denote len(subroot) and len(root) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @leokim0922 λμ μ νμλ, μκ° λ³΅μ‘λλ₯Ό There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄κ² μκ° λ³΅μ‘λ O(m+n)λ κ°λ₯ν μ€ λͺ°λλ€μ. μμ§ κ° κΈΈμ΄ λ©κ΅°μ. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hint: μ΄λ»κ² νλ©΄ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ν μ΄λ―Έ λ°©λ¬Έν nodeλ₯Ό visited μ²λ¦¬ν΄μ μλλ©΄ λ κ±°κ°μλ° κ³ λ―Όμ μ’ ν΄λ΄μΌν κ±°κ°λ€μ π |
||
## SC: O(m+n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. π |
Uh oh!
There was an error while loading. Please reload this page.