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.

Example 1:
Given intervals [1,3],[6,9], insert and merge [2,5] in as [1,5],[6,9].

Example 2:
Given [1,2],[3,5],[6,7],[8,10],[12,16], insert and merge [4,9] in as [1,2],[3,10],[12,16].

This is because the new interval [4,9] overlaps with [3,5],[6,7],[8,10].

一道区间类的题目,和merge intervals是姊妹题.

这题给出已经按照开始时间排序的原始区间序列,之后插入一个新的区间.

原有区间和插入区间一共有四种关系:

for each interval I(i)in intervals, there're 4 situations: 1. I(i) is before newInterval, insert I(i) directly

                                                                             2. I(i) has a overlap with newInterval, merge them to be the newInterval

                                                                             3. newInterval is before I(i), insert newInterval, then I(i)

                                                                             4. I(i) is after the newInterval, insert directly.

主要在于第二种情况如何判断是否有overlap,可以从反面出发,如果a,b两个区间不重合则 a.start > b.end or b.start > a.end ,起点和终点之间有重复.所以反之, not(a.start > b.end or b.start > a.end) = s.start <= b.end and b.start <= a.end. 注意交叠的区间a,b合并结果为[min(a.start,b.start), max(a.end, b.end)]

可得阶段处理的代码如下:

# Definition for an interval.
# class Interval(object):
#     def __init__(self, s=0, e=0):
#         self.start = s
#         self.end = e

class Solution(object):
    def insert(self, intervals, newInterval):
        """
        :type intervals: List[Interval]
        :type newInterval: Interval
        :rtype: List[Interval]
        """
        """
        steps:
        for each interval I(i)in intervals, there're 4 situations: 1. I(i) is before newInterval, insert I(i) directly
                                                                   2. I(i) has a overlap with newInterval, merge them to be the newInterval
                                                                   3. newInterval is before I(i), insert newInterval, then I(i)
                                                                   4. I(i) is after the newInterval, insert directly.
        """
        if not newInterval:
            return intervals
        res = []
        i = 0
        while i < len(intervals) and intervals[i].end < newInterval.start:  #before
            res.append(intervals[i])
            i += 1
        while i < len(intervals) and intervals[i].start <= newInterval.end: #overlap 第一个while不通过,则newIntervals.start <= intervals[i].end 两个一起合成条件
            newInterval.start = min(newInterval.start, intervals[i].start)  #全部合并到新区间上
            newInterval.end = max(newInterval.end, intervals[i].end)
            i += 1
        res.append(newInterval)
        while i < len(intervals) and intervals[i].start > newInterval.end:  #after
            res.append(intervals[i])
            i += 1
        return res

 

posted on 2016-08-29 16:16  Sheryl Wang  阅读(158)  评论(0编辑  收藏  举报

导航