ZhangZhihui's Blog  

You are given an array of non-overlapping intervals intervals where intervals[i] = [starti, endi] represent the start and the end of the ith interval and intervals is sorted in ascending order by starti. You are also given an interval newInterval = [start, end] that represents the start and end of another interval.

Insert newInterval into intervals such that intervals is still sorted in ascending order by starti and intervals still does not have any overlapping intervals (merge overlapping intervals if necessary).

Return intervals after the insertion.

Note that you don't need to modify intervals in-place. You can make a new array and return it.

 

Example 1:

Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]

Example 2:

Input: intervals = [[1,2],[3,5],[6,7],[8,10],[12,16]], newInterval = [4,8]
Output: [[1,2],[3,10],[12,16]]
Explanation: Because the new interval [4,8] overlaps with [3,5],[6,7],[8,10].

 

Constraints:

  • 0 <= intervals.length <= 104
  • intervals[i].length == 2
  • 0 <= starti <= endi <= 105
  • intervals is sorted by starti in ascending order.
  • newInterval.length == 2
  • 0 <= start <= end <= 105

 

My Solution:

class Solution(object):
    def insert(self, intervals, newInterval):
        """
        :type intervals: List[List[int]]
        :type newInterval: List[int]
        :rtype: List[List[int]]
        """

        res_left = []
        res_right = []

        while intervals:
            # out of the left
            if newInterval[1] < intervals[0][0]:
                return res_left + [newInterval] + intervals + res_right
            # out of the right
            elif newInterval[0] > intervals[-1][1]:
                return res_left + intervals + [newInterval] + res_right
            # covers
            elif newInterval[0] <= intervals[0][0] and newInterval[1] >= intervals[-1][1]:
                return res_left + [newInterval] + res_right
            # bridges
            elif intervals[0][0] <= newInterval[0] <= intervals[0][1]  and  intervals[-1][0] <= newInterval[1] <= intervals[-1][1]:
                return res_left + [[intervals[0][0], intervals[-1][1]]] + res_right
            # between
            else:
                # on the right of the first
                if newInterval[0] > intervals[0][1]:
                    res_left.append(intervals.pop(0))
                # overlap
                else:
                    intervals[0] = [min(intervals[0][0], newInterval[0]), max(intervals[0][1], newInterval[1])]
                
                # on the left of the end
                if newInterval[1] < intervals[-1][0]:
                    res_right.insert(0, intervals.pop())
                # overlap
                else:
                    intervals[-1] = [min(intervals[-1][0], newInterval[0]), max(intervals[-1][1], newInterval[1])]

        return res_left + [newInterval] + res_right

 

 

posted on 2025-03-19 13:33  ZhangZhihuiAAA  阅读(12)  评论(0)    收藏  举报