Insert Interval

Problem Id: 57 Difficulty: Medium Tag: Array Tag: Sort


The new interval would connect all the intervals where start in the new interval.


class Solution:
    def insert(self, intervals: List[List[int]], newInterval: List[int]) -> List[List[int]]:
        start, end = newInterval
        ans = []
        for i, j in intervals:
            if j < start:
                ans.append([i, j])
            elif i > end:
                ans.append([start, end])
                start, end = float('inf'), float('inf')
                ans.append([i, j])
                start = min(start, i)
                end = max(end, j)
        if start != float('inf'):
            ans.append([start, end])
        return ans