[LeetCode] Insert Interval
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]
.
解题思路:
题意为向一个已有的排序区间插入一个新区间,运行必要的合并。使得结构中没有重合的区间。
解法1,先将待插入的区间插入到原来的区间数组中,然后按Merge Interval的办法运行(http://www.kangry.net/blog/?
type=article&article_id=337)。
可是这种时间复杂度为O(nlogn)。没实用到原区间数组已经排好序这个信息。
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ class Solution { public: vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) { intervals.push_back(newInterval); std::sort(intervals.begin(), intervals.end(), comp); vector<Interval> result; int len = intervals.size(); for(int i=0; i<len; i++){ if(result.size() == 0 || !isOver(result[result.size()-1], intervals[i])){ result.push_back(intervals[i]); }else{ result[result.size()-1].end = max(result[result.size()-1].end, intervals[i].end); } } return result; } static bool comp(Interval& interval1, Interval& interval2){ return interval1.start < interval2.start; } bool isOver(Interval& interval1, Interval& interval2){ return interval1.start<=interval2.end && interval1.end>=interval2.start; } };解法2、扫描区间数组,分情况讨论。
(1)若新区间没有插入,且新区间的起始节点小于当前扫描区间的起始节点,那么将新区间插入
(2)若新区间没有插入。且新区间的起始节点大于当前扫描区间,而且当前扫描区间与新区间没有重合,则插入当前扫描区间
(3)若新区间没有插入。且新区间的起始节点大于当前扫描区间。但当前扫描区间与新区间有重合。则将当前扫描区间与新区间合并后插入到结果中
(4)若新区间已经插入,则按Merge Intereval的方法处理
注意到扫描结束后还应该推断新区间是否已经插入。
/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(s), end(e) {} * }; */ class Solution { public: vector<Interval> insert(vector<Interval>& intervals, Interval newInterval) { vector<Interval> result; int len = intervals.size(); bool flag = false; //newInterval是否增加到了result中了 for(int i=0; i<len; i++){ if(!flag){ if(newInterval.start < intervals[i].start){ result.push_back(newInterval); flag = true; i--; }else if(!isOver(newInterval, intervals[i])){ result.push_back(intervals[i]); }else{ newInterval.start = min(newInterval.start, intervals[i].start); newInterval.end = max(newInterval.end, intervals[i].end); result.push_back(newInterval); flag = true; } }else if(!isOver(result[result.size() - 1], intervals[i])){ result.push_back(intervals[i]); }else{ result[result.size() - 1].end = max(result[result.size() - 1].end, intervals[i].end); } } if(!flag){ if(result.size() == 0 || !isOver(result[result.size() - 1], newInterval)){ result.push_back(newInterval); }else{ result[result.size() - 1].end = max(result[result.size() - 1].end, newInterval.end); } } return result; } bool isOver(Interval& interval1, Interval& interval2){ return interval1.start<=interval2.end && interval1.end>=interval2.start; } };