Skip to content

Commit bca6687

Browse files
committed
[bhyun-kim, 김병현] Week 13 Solutions
1 parent 3ca64ba commit bca6687

File tree

5 files changed

+221
-0
lines changed

5 files changed

+221
-0
lines changed

insert-interval/bhyun-kim.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
57. Insert Interval
3+
https://leetcode.com/problems/insert-interval/
4+
5+
Solution:
6+
To solve this problem, we can follow the following steps:
7+
1. Create an empty list called result to store the final intervals.
8+
2. Initialize a variable i to 0 to iterate through the intervals.
9+
3. Iterate through the intervals and add all intervals ending before the new interval starts to the result list.
10+
11+
Time Complexity: O(n)
12+
-
13+
14+
"""
15+
16+
17+
from typing import List
18+
19+
20+
class Solution:
21+
def insert(
22+
self, intervals: List[List[int]], newInterval: List[int]
23+
) -> List[List[int]]:
24+
result = []
25+
i = 0
26+
n = len(intervals)
27+
28+
# Add all intervals ending before the new interval starts
29+
while i < n and intervals[i][1] < newInterval[0]:
30+
result.append(intervals[i])
31+
i += 1
32+
33+
# Merge all overlapping intervals with the new interval
34+
while i < n and intervals[i][0] <= newInterval[1]:
35+
newInterval[0] = min(newInterval[0], intervals[i][0])
36+
newInterval[1] = max(newInterval[1], intervals[i][1])
37+
i += 1
38+
result.append(newInterval)
39+
40+
# Add all intervals starting after the new interval ends
41+
while i < n:
42+
result.append(intervals[i])
43+
i += 1
44+
45+
return result

meeting-rooms-ii/bhyun-kim.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
253. Meeting Rooms II
3+
https://leetcode.com/problems/meeting-rooms-ii/
4+
5+
Solution:
6+
To solve this problem, we can follow the following steps:
7+
1. Sort the intervals based on their start times.
8+
2. Initialize a list called room_times with a dummy interval.
9+
3. Iterate through the intervals and assign each meeting to a room.
10+
4. If a meeting can be assigned to an existing room, update the room's end time.
11+
5. If a meeting cannot be assigned to an existing room, add a new room.
12+
6. Return the number of rooms used.
13+
14+
Time Complexity: O(nlogn)
15+
- Sorting the intervals takes O(nlogn) time.
16+
- Iterating through the intervals takes O(n) time.
17+
- Overall, the time complexity is O(nlogn).
18+
19+
Space Complexity: O(n)
20+
- We are using a list to store the room times.
21+
"""
22+
23+
from typing import List
24+
25+
26+
class Solution:
27+
def minMeetingRooms(self, intervals: List[List[int]]) -> int:
28+
intervals = sorted(intervals)
29+
room_times = [[-1, -1]]
30+
meet_idx = 0
31+
32+
while meet_idx < len(intervals):
33+
m_t = intervals[meet_idx]
34+
found_room = False
35+
for i in range(len(room_times)):
36+
r_t = room_times[i]
37+
if m_t[0] >= r_t[1]:
38+
room_times[i] = m_t
39+
found_room = True
40+
break
41+
42+
if not found_room:
43+
room_times.append(m_t)
44+
meet_idx += 1
45+
46+
return len(room_times)

merge-intervals/bhyun-kim.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
56. Merge Intervals
3+
https://leetcode.com/problems/merge-intervals/
4+
5+
Solution:
6+
To solve this problem, we can follow the following steps:
7+
1. Sort the intervals by their start times.
8+
2. Initialize an empty list called merged to store the final merged intervals.
9+
3. Iterate through the sorted intervals and merge the current interval with the previous interval if there is an overlap.
10+
11+
Time Complexity: O(nlogn)
12+
- Sorting the intervals takes O(nlogn) time.
13+
- Merging the intervals takes O(n) time.
14+
- Overall, the time complexity is O(nlogn).
15+
16+
Space Complexity: O(n)
17+
- The space complexity is O(n) to store the merged intervals.
18+
"""
19+
20+
from typing import List
21+
22+
23+
class Solution:
24+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
25+
if not intervals:
26+
return []
27+
28+
# Sort intervals by their start times
29+
intervals.sort(key=lambda x: x[0])
30+
31+
merged = []
32+
for interval in intervals:
33+
# if the list of merged intervals is empty or if the current interval does not overlap with the previous
34+
if not merged or merged[-1][1] < interval[0]:
35+
merged.append(interval)
36+
else:
37+
# there is an overlap, so we merge the current and previous intervals
38+
merged[-1][1] = max(merged[-1][1], interval[1])
39+
40+
return merged
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
435. Non-overlapping Intervals
3+
https://leetcode.com/problems/non-overlapping-intervals/
4+
5+
Solution:
6+
To solve this problem, we can follow the following steps:
7+
1. Sort the intervals based on their end times.
8+
2. Initialize the end time of the last added interval.
9+
3. Iterate through the intervals and add non-overlapping intervals to the count.
10+
4. The number of intervals to remove is the total number minus the count of non-overlapping intervals.
11+
12+
Time Complexity: O(nlogn)
13+
- Sorting the intervals takes O(nlogn) time.
14+
- Iterating through the intervals takes O(n) time.
15+
- Overall, the time complexity is O(nlogn).
16+
17+
Space Complexity: O(1)
18+
- We are using a constant amount of space to store the end time of the last added interval.
19+
"""
20+
21+
from typing import List
22+
23+
24+
class Solution:
25+
def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
26+
if not intervals:
27+
return 0
28+
29+
# Sort intervals based on end times
30+
intervals.sort(key=lambda x: x[1])
31+
32+
# Initialize the end time of the last added interval
33+
end = intervals[0][1]
34+
count = 1
35+
36+
for i in range(1, len(intervals)):
37+
if intervals[i][0] >= end:
38+
count += 1
39+
end = intervals[i][1]
40+
41+
# The number of intervals to remove is the total number minus the count of non-overlapping intervals
42+
return len(intervals) - count

rotate-image/bhyun-kim.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
48. Rotate Image
3+
https://leetcode.com/problems/rotate-image/
4+
5+
Solution:
6+
To solve this problem, we can follow the following steps:
7+
1. Process layers from the outermost to the innermost.
8+
2. For each layer, iterate through the elements in the layer.
9+
3. Swap the elements in the layer in a clockwise direction.
10+
4. Repeat the process for all layers.
11+
12+
Time Complexity: O(n^2)
13+
- We need to process all elements in the matrix.
14+
- The time complexity is O(n^2).
15+
16+
Space Complexity: O(1)
17+
- We are rotating the matrix in place without using any extra space.
18+
"""
19+
20+
from typing import List
21+
22+
23+
class Solution:
24+
def rotate(self, matrix: List[List[int]]) -> None:
25+
"""
26+
Do not return anything, modify matrix in-place instead.
27+
"""
28+
n = len(matrix)
29+
# Process layers from the outermost to the innermost
30+
for layer in range(n // 2):
31+
first = layer
32+
last = n - layer - 1
33+
for i in range(first, last):
34+
offset = i - first
35+
# Save the top element
36+
top = matrix[first][i]
37+
38+
# Move left element to top
39+
matrix[first][i] = matrix[last - offset][first]
40+
41+
# Move bottom element to left
42+
matrix[last - offset][first] = matrix[last][last - offset]
43+
44+
# Move right element to bottom
45+
matrix[last][last - offset] = matrix[i][last]
46+
47+
# Assign top element to right
48+
matrix[i][last] = top

0 commit comments

Comments
 (0)