Skip to content

Commit 367d4da

Browse files
committed
[bhyun-kim, 김병현] Solution of Week 3 Assignments
1 parent 0db7eba commit 367d4da

File tree

6 files changed

+256
-1
lines changed

6 files changed

+256
-1
lines changed

climbing-stairs/bhyun-kim.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""
2+
70. Climbing Stairs
3+
https://leetcode.com/problems/climbing-stairs/description/
4+
5+
Solution:
6+
This is a combinatorial problem that allows duplicates.
7+
We can use the formula for combinations to solve this problem.
8+
Let's suppose that n is the number of steps, and k is the number of 2-steps.
9+
Then, the number of ways to climb the stairs is n!/((n-2k)!k!).
10+
We can iterate through all possible values of k, and calculate the number of ways to climb the stairs.
11+
12+
1. Create a dictionary to store the factorials of numbers from 0 to n.
13+
2. Calculate the factorials of numbers from 0 to n.
14+
3. Iterate through all possible values of k from 0 to n//2.
15+
16+
17+
Time complexity: O(n)
18+
Space complexity: O(n)
19+
"""
20+
21+
from collections import defaultdict
22+
23+
24+
class Solution:
25+
def climbStairs(self, n: int) -> int:
26+
factorials = defaultdict(int)
27+
factorials[0] = 1
28+
29+
fact = 1
30+
for i in range(1, n + 1):
31+
fact = fact * i
32+
factorials[i] = fact
33+
34+
output = 0
35+
36+
for i in range(n // 2 + 1):
37+
num_ways = factorials[n - i] / factorials[n - (i * 2)] / factorials[i]
38+
output += num_ways
39+
return int(output)

invert-binary-tree/bhyun-kim.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
3131
has_right = hasattr(root, "right")
3232

3333
if has_left and has_right:
34-
root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
34+
root.left, root.right = self.invertTree(root.right), self.invertTree(
35+
root.left
36+
)
3537
elif has_left:
3638
root.left, root.right = None, self.invertTree(root.left)
3739
elif has_right:
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
104. Maximum Depth of Binary Tree
3+
https://leetcode.com/problems/maximum-depth-of-binary-tree/
4+
5+
Solution:
6+
This is a recursive problem that requires traversing the binary tree.
7+
We can use a recursive function to traverse the binary tree and calculate the depth.
8+
The depth of the binary tree is the maximum of the depth of the left and right subtrees.
9+
We can calculate the depth of the left and right subtrees recursively.
10+
The base case is when the root is None, in which case the depth is 0.
11+
12+
1. Calculate the depth of the left subtree.
13+
2. Calculate the depth of the right subtree.
14+
3. Return the maximum of the depth of the left and right subtrees plus 1.
15+
16+
Time complexity: O(n)
17+
Space complexity: O(n)
18+
"""
19+
20+
21+
from typing import Optional
22+
23+
24+
class TreeNode:
25+
def __init__(self, val=0, left=None, right=None):
26+
self.val = val
27+
self.left = left
28+
self.right = right
29+
30+
31+
class Solution:
32+
def maxDepth(self, root: Optional[TreeNode]) -> int:
33+
max_dep_left = 0
34+
max_dep_right = 0
35+
36+
if hasattr(root, "left"):
37+
max_dep_left = 1 + self.maxDepth(root.left)
38+
if hasattr(root, "right"):
39+
max_dep_right = 1 + self.maxDepth(root.right)
40+
41+
return max(max_dep_left, max_dep_right)

meeting-rooms/bhyun-kim.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""
2+
252. Meeting Rooms
3+
https://leetcode.com/problems/meeting-rooms/
4+
5+
Solution:
6+
This is a sorting problem that requires comparing intervals.
7+
We can sort the intervals by the start time.
8+
Then, we can iterate through the sorted intervals and check if the end time of the current interval is greater than the start time of the next interval.
9+
If it is, then we return False.
10+
Otherwise, we return True.
11+
12+
1. Sort the intervals by the start time.
13+
2. Iterate through the sorted intervals.
14+
3. Check if the end time of the current interval is greater than the start time of the next interval.
15+
16+
17+
Time complexity: O(nlogn)
18+
Space complexity: O(1)
19+
20+
Discarded solution:
21+
This solution uses a hash table to store the time table of the intervals.
22+
We iterate through the intervals and check if there is any overlap in the time table.
23+
24+
1. Create a hash table to store the time table of the intervals.
25+
2. Iterate through the intervals.
26+
3. Check if there is any overlap in the time table.
27+
4. Return False if there is an overlap, otherwise return True.
28+
29+
Time complexity: O(n^2)
30+
Space complexity: O(n)
31+
32+
class Solution:
33+
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
34+
35+
time_table = defaultdict(int)
36+
for itrv in intervals:
37+
for i in range(itrv[0], itrv[1]):
38+
if time_table[i] == 0:
39+
time_table[i] += 1
40+
else:
41+
return False
42+
return True
43+
"""
44+
45+
46+
from typing import List
47+
48+
49+
class Solution:
50+
def canAttendMeetings(self, intervals: List[List[int]]) -> bool:
51+
intervals.sort()
52+
for i in range(len(intervals) - 1):
53+
if intervals[i][1] > intervals[i + 1][0]:
54+
return False
55+
return True

same-tree/bhyun-kim.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
100. Same Tree
3+
https://leetcode.com/problems/same-tree/description/
4+
5+
Solution:
6+
This is a recursive problem that requires comparing two binary trees.
7+
We can use a recursive function to compare the values of the nodes in the two binary trees.
8+
If the values of the nodes are equal, we can compare the left and right subtrees recursively.
9+
The base case is when both nodes are None, in which case we return True.
10+
11+
1. Check if the values of the nodes are equal.
12+
2. Compare the left and right subtrees recursively.
13+
3. Return True if the values of the nodes are equal and the left and right subtrees are equal, otherwise return False.
14+
"""
15+
16+
17+
from typing import Optional
18+
19+
20+
# Definition for a binary tree node.
21+
class TreeNode:
22+
def __init__(self, val=0, left=None, right=None):
23+
self.val = val
24+
self.left = left
25+
self.right = right
26+
27+
28+
class Solution:
29+
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
30+
if p == q == None:
31+
return True
32+
33+
if hasattr(p, "val") and hasattr(q, "val"):
34+
if p.val != q.val:
35+
return False
36+
37+
left_same = self.isSameTree(p.left, q.left)
38+
right_same = self.isSameTree(p.right, q.right)
39+
40+
return left_same == right_same == True
41+
42+
else:
43+
return False

subtree-of-another-tree/bhyun-kim.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
572. Subtree of Another Tree
3+
https://leetcode.com/problems/subtree-of-another-tree/description/
4+
5+
Solution:
6+
This solution uses a depth-first search to find the subtree in the tree.
7+
We can use a recursive function to traverse the tree and compare the nodes.
8+
If the given node is identical to the subtree, we return True.
9+
Otherwise, we go to the left and right subtrees recursively.
10+
In this solution we use two helper functions: depth_first_search and is_identical.
11+
12+
Depth-first search:
13+
1. Check if the root is None.
14+
2. Check if the root is identical to the subtree.
15+
3. Go to the left and right subtrees recursively.
16+
17+
Is identical:
18+
1. Check if both nodes are None.
19+
2. Check if one of the nodes is None.
20+
3. Check if the values of the nodes are equal.
21+
4. Compare the left and right subtrees recursively.
22+
23+
24+
Complexity analysis:
25+
Time complexity: O(n*m)
26+
Where n is the number of nodes in the tree and m is the number of nodes in the subtree.
27+
The depth-first search function has a time complexity of O(n).
28+
The is_identical function has a time complexity of O(m).
29+
Therefore, the overall time complexity is O(n*m).
30+
31+
Space complexity: O(n)
32+
Where n is the number of nodes in the tree.
33+
The space complexity is O(n) because of the recursive calls to the depth_first_search function.
34+
"""
35+
36+
37+
from typing import Optional
38+
39+
40+
# Definition for a binary tree node.
41+
class TreeNode:
42+
def __init__(self, val=0, left=None, right=None):
43+
self.val = val
44+
self.left = left
45+
self.right = right
46+
47+
48+
class Solution:
49+
def isSubtree(self, root: Optional[TreeNode], subRoot: Optional[TreeNode]) -> bool:
50+
def depth_first_search(root):
51+
if root is None:
52+
return False
53+
54+
if is_identical(root, subRoot):
55+
return True
56+
57+
return depth_first_search(root.left) or depth_first_search(root.right)
58+
59+
def is_identical(root1, root2):
60+
if root1 is None and root2 is None:
61+
return True
62+
if root1 is not None and root2 is None:
63+
return False
64+
if root1 is None and root2 is not None:
65+
return False
66+
if root1.val == root2.val:
67+
return (
68+
is_identical(root1.left, root2.left)
69+
== is_identical(root1.right, root2.right)
70+
== True
71+
)
72+
else:
73+
return False
74+
75+
return depth_first_search(root)

0 commit comments

Comments
 (0)