Skip to content

Commit e5a39a6

Browse files
committed
[Leo] 3rd Week solutions
1 parent d769f97 commit e5a39a6

File tree

5 files changed

+80
-0
lines changed

5 files changed

+80
-0
lines changed

climbing-stairs/Leo.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def climbStairs(self, n: int) -> int:
3+
fast, slow = 1, 1
4+
5+
for i in range(n - 1):
6+
tmp = fast
7+
fast = fast + slow
8+
slow = tmp
9+
10+
return fast
11+
12+
## TC: O(n), SC(1)

maximum-depth-of-binary-tree/Leo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def maxDepth(self, root: Optional[TreeNode]) -> int:
9+
if not root:
10+
return 0
11+
12+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
13+
14+
## TC: O(n), SC: O(n) or O(logn) if it is balanced

meeting-rooms/Leo.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
3+
intervals.sort() ## nlogn
4+
5+
for i in range(len(intervals) - 1):
6+
if intervals[i][1] > intervals[i+1][0]:
7+
return False
8+
9+
return True
10+
11+
## TC: n(nlogn), SC: O(1)

same-tree/Leo.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
9+
if p and q:
10+
return p.val == q.val and self.isSameTree(p.right, q.right) and self.isSameTree(p.left, q.left)
11+
else:
12+
return p == q
13+
14+
## TC: O(n), SC: O(n) or O(logn) if it is balanced

subtree-of-another-tree/Leo.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
9+
def dfs(root, subroot):
10+
if not root and not subroot:
11+
return True
12+
if not root or not subroot:
13+
return False
14+
if root.val != subroot.val:
15+
return False
16+
17+
return dfs(root.left, subroot.left) and dfs(root.right, subroot.right)
18+
19+
def solve(root, subroot):
20+
if not root:
21+
return False
22+
if dfs(root, subroot):
23+
return True
24+
return solve(root.left, subroot) or solve(root.right, subroot)
25+
26+
return solve(root, subRoot)
27+
28+
## TC: O(mn), where m and n denote len(subroot) and len(root)
29+
## SC: O(m+n)

0 commit comments

Comments
 (0)