Skip to content

Commit 4b7905f

Browse files
authored
Merge pull request DaleStudy#74 from leokim0922/main
[Leo] 3rd Week solutions
2 parents 227f589 + 806acb3 commit 4b7905f

File tree

5 files changed

+81
-0
lines changed

5 files changed

+81
-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:O(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: O(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 is None and q is None
13+
14+
## TC: O(n), SC: O(n) or O(logn) if it is balanced

subtree-of-another-tree/Leo.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
10+
def dfs(root, subRoot):
11+
if not root and not subRoot:
12+
return True
13+
if not root or not subRoot:
14+
return False
15+
if root.val != subRoot.val:
16+
return False
17+
18+
return dfs(root.left, subRoot.left) and dfs(root.right, subRoot.right)
19+
20+
def solve(root):
21+
if not root:
22+
return False
23+
if dfs(root, subRoot):
24+
return True
25+
return solve(root.left) or solve(root.right)
26+
27+
return solve(root)
28+
29+
## TC: O(mn), where m and n denote len(subroot) and len(root)
30+
## SC: O(m+n)

0 commit comments

Comments
 (0)