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、只是新添加了一个时间段在其中,不影响原有的时间段;
2、影响了一个时间段;
2、合并了两个或多个时间段;
这样看来,只需要按顺序遍历一次有序的时间段集合即可。
/** * 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>::iterator it = intervals.begin(); while(it != intervals.end()) { if(newInterval.end < it->start) { intervals.insert(it, newInterval); return intervals; } else if(newInterval.start > it->end) { it++; } else //发现重叠 { newInterval.start = min(newInterval.start, it->start); newInterval.end = max(newInterval.end, it->end); it = intervals.erase(it); //删除it所指结点,返回下一个结点位置 } } intervals.insert(intervals.end(), newInterval); return intervals; } };上面的代码看似时间复杂度是O(N),实际上对vector的erase的每一次操作都是O(N)的,因此总的时间复杂度是O(N^2)。为了避免这样,不再原vector上做修改,而是新建一个新的vector作为新的时间段集合。以空间换取时间,这样时间和空间复杂度都为O(N)。
/** * 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; vector<Interval>::iterator it = intervals.begin(); while(it != intervals.end()) { if(newInterval.end < it->start) { break; } else if(newInterval.start > it->end) { result.push_back(*it); it++; } else //it结点和新时间段重叠,it结点被时间段影响 { newInterval.start = min(newInterval.start, it->start); newInterval.end = max(newInterval.end, it->end); it++; } } result.push_back(newInterval); for(;it!=intervals.end();++it) result.push_back(*it); return result; } };
/** * 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) { multimap<int, int> newlist; newlist.insert(make_pair(newInterval.start, 1)); newlist.insert(make_pair(newInterval.end, 0)); vector<Interval>::iterator it = intervals.begin(); for(;it != intervals.end();it++) { newlist.insert(make_pair(it->start, 1)); newlist.insert(make_pair(it->end, 0)); } intervals.clear(); multimap<int, int>::iterator it2 = newlist.begin(); int left, right, count; count = 0; left = it2->first; while(it2 != newlist.end()) { if(it2->second == 1) count++; else count--; if(count == 0) { right = it2->first; it2++; if(it2 != newlist.end() && right == it2->first) { it2++; count++; continue; } Interval tmp = Interval(left, right); intervals.push_back(tmp); if(it2 != newlist.end()) left = it2->first; } else { it2++; } } return intervals; } };