Skip to content

Commit dc29245

Browse files
committed
[:solved] #277
1 parent 8ad7c17 commit dc29245

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

insert-interval/ppxyn1.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# idea: -
2+
# Time Complexity: O(n)
3+
class Solution:
4+
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
5+
res = []
6+
7+
# Ex,. newInterval = [2,5]
8+
for i in range(len(intervals)):
9+
start, end = intervals[i]
10+
11+
# [2,5] > [1,5]
12+
13+
# already passed
14+
if end < newInterval[0]:
15+
res.append(intervals[i])
16+
17+
# not started yet
18+
# (2)
19+
elif newInterval[1] < start:
20+
res.append(newInterval) # [1,5]
21+
for j in range(i, len(intervals)):
22+
res.append(intervals[j])
23+
return res
24+
else:
25+
# (1)
26+
# overlapping
27+
newInterval[0] = min(newInterval[0], start)
28+
newInterval[1] = max(newInterval[1], end)
29+
30+
res.append(newInterval)
31+
return res
32+

0 commit comments

Comments
 (0)