Interval"
Since we only have to insert one interval here, and our intervals are already sorted, we can do this in O(n) time by thinking about it as having three sections:
def main(intervals, newInterval):
= []
left = []
right = [newInterval[0], newInterval[1]]
mid
for interval in intervals:
if interval[1] < start:
left.append(interval)elif interval[0] > end:
right.append(interval)else:
0] = min(mid[0], interval[0])
mid[1] = max(mid[1], interval[1])
mid[
return left + [mid] + right