Skip to content

[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

Merged
merged 2 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
12 changes: 12 additions & 0 deletions climbing-stairs/Leo.py
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)
14 changes: 14 additions & 0 deletions maximum-depth-of-binary-tree/Leo.py
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
11 changes: 11 additions & 0 deletions meeting-rooms/Leo.py
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)
14 changes: 14 additions & 0 deletions same-tree/Leo.py
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
30 changes: 30 additions & 0 deletions subtree-of-another-tree/Leo.py
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)
Copy link
Member

Choose a reason for hiding this comment

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

@leokim0922 λ‹˜μ€ 잘 ν•˜μ‹œλ‹ˆ, μ‹œκ°„ λ³΅μž‘λ„λ₯Ό $O(m + n)$으둜 κ°œμ„ ν•  수 μžˆλŠ” 방법도 κ³ λ―Όν•΄λ³΄μ‹œλ©΄ 쒋을 것 κ°™μŠ΅λ‹ˆλ‹€. πŸ˜„

Copy link
Contributor

Choose a reason for hiding this comment

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

이게 μ‹œκ°„ λ³΅μž‘λ„ O(m+n)도 κ°€λŠ₯ν•œ 쀄 λͺ°λžλ„€μš”. 아직 갈 길이 λ©€κ΅°μš”.

Copy link
Member

Choose a reason for hiding this comment

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

Hint: μ–΄λ–»κ²Œ ν•˜λ©΄ subRootλ₯Ό λŒ€μƒμœΌλ‘œ 쀑볡 μˆœνšŒν•˜λŠ” 것을 λ°©μ§€ν•  수 μžˆμ„κΉŒ? 🧠

Copy link
Contributor Author

Choose a reason for hiding this comment

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

흠 이미 λ°©λ¬Έν•œ nodeλ₯Ό visited μ²˜λ¦¬ν•΄μ„œ μ•ˆλŒλ©΄ 될거같은데 고민을 μ’€ ν•΄λ΄μ•Όν• κ±°κ°™λ„€μš” πŸ˜“

## SC: O(m+n)
Copy link
Member

Choose a reason for hiding this comment

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

πŸ‘