Skip to content

Commit 475c43c

Browse files
committed
[Leo] 13th Week solutions
1 parent 3ca64ba commit 475c43c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

insert-interval/Leo.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution:
2+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
3+
left = []
4+
right = []
5+
6+
for interval in intervals:
7+
if interval[1] < newInterval[0]:
8+
left.append(interval)
9+
elif interval[0] > newInterval[1]:
10+
right.append(interval)
11+
else:
12+
newInterval[0] = min(newInterval[0], interval[0])
13+
newInterval[1] = max(newInterval[1], interval[1])
14+
15+
return left + [newInterval] + right
16+
17+
## TC: O(n), SC: O(n)

merge-intervals/Leo.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def merge(self, intervals: List[List[int]]) -> List[List[int]]:
3+
def sort_key(interval):
4+
return interval[0]
5+
6+
intervals.sort(key=sort_key)
7+
8+
result = [intervals[0]]
9+
10+
for start, end in intervals[1:]:
11+
lastEnd = result[-1][1]
12+
13+
if start <= lastEnd:
14+
result[-1][1] = max(lastEnd, end)
15+
else:
16+
result.append([start, end])
17+
18+
return result
19+
20+
## TC: O(nlogn), SC: O(n)

0 commit comments

Comments
 (0)