Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Tags: Interval
Try It!
Discussion
Video
Solution
class Solution:
def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
left = []
right = []
for cur in intervals:
if cur[1] < newInterval[0]:
left.append(cur)
elif cur[0] > newInterval[1]:
right.append(cur)
else:
newInterval[0] = min(newInterval[0], cur[0])
newInterval[1] = max(newInterval[1], cur[1])
return left + [newInterval] + right