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]
.
思路一:找到overlap发生的left和right,然后将left~right直接的做合并,两侧的直接加入到结果中。
不知道为什么结果是
Submission Result: Output Limit Exceeded
/** * 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) { size_t n = intervals.size(); vector<Interval> res; vector<bool> valid(n, true); Interval merge; if(n == 0) { res.push_back(newInterval); return res; } #if 0 for(int i = 0; i < intervals.size(); i++) { cout << "[" << intervals[i].start << "," << intervals[i].end << "] " ; } cout << endl; #endif int left = 0, right = 0; for(int i = 0; i < n; i++) { if(newInterval.start < intervals[i].start) { left = i - 1; break; } } if(left == -1 || intervals[left].end < newInterval.start) { left ++; merge.start = newInterval.start; } else { merge.start = intervals[left].start; } for(int i = left; i < n; i++) { if(newInterval.end < intervals[i].start) { right = i - 1; break; } } if(right == -1 || intervals[right].end < newInterval.end) { merge.end = newInterval.end; } else { merge.end = intervals[right].end; } for(int i = 0; i < left; i++) { res.push_back(intervals[i]); } res.push_back(merge); for(int i = right+1; i < n; i++) { res.push_back(intervals[i]); } return res; } };
对上诉code进行分析,发现当newInterval在最后的时候处理的不正确,即当没有interals[i].start > newInterval.start时,处理不正确。修改,加了红色的字体部分后终于AC了。
之后我想 Submission Result: Output Limit Exceeded 的意思应该是输出的东西比expected的多吧。。 总之AC了很开心
class Solution { public: vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { size_t n = intervals.size(); vector<Interval> res; Interval merge; if(n == 0) { res.push_back(newInterval); return res; } #if 0 for(int i = 0; i < intervals.size(); i++) { cout << "[" << intervals[i].start << "," << intervals[i].end << "] " ; } cout << endl; #endif int left = 0, right = 0, i; for(i = 0; i < n; i++) { if(newInterval.start < intervals[i].start) { left = i - 1; break; } } if(i == n) //new should be insert at the last { left = n-1; } if(left == -1 || intervals[left].end < newInterval.start) { left ++; merge.start = newInterval.start; } else { merge.start = intervals[left].start; } right = left; for(i = left; i < n; i++) { if(newInterval.end < intervals[i].start) { right = i - 1; break; } } if(i == n) //new should be insert at the last { right = n-1; } if(right == -1 || intervals[right].end < newInterval.end) { merge.end = newInterval.end; } else { merge.end = intervals[right].end; } //cout << "left " << left << endl; //cout << "right " << right << endl; for(i = 0; i < left; i++) { res.push_back(intervals[i]); } res.push_back(merge); for(i = right+1; i < n; i++) { res.push_back(intervals[i]); } return res; } };
方法二:直接服用merge函数就AC了,就算偷懒了吧
bool cmp(Interval i, Interval j) { return i.start < j.start; } class Solution { public: //just reuse the solution of "Merge Intervals", quite straight forward vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) { intervals.push_back(newInterval); return merge(intervals); } vector<Interval> merge(vector<Interval> &intervals) { size_t n = intervals.size(); vector<bool> valid(n, true); vector<Interval> res; sort(intervals.begin(), intervals.end(), cmp); #if 0 for(int i = 0; i < intervals.size(); i++) { cout << "[" << intervals[i].start << "," << intervals[i].end << "] " ; } cout << endl; #endif for(int i = 1; i < n; i++) { Interval & pre = intervals[i-1]; Interval & cur = intervals[i]; if(pre.end >= cur.start) { valid[i-1] = false; cur.start = pre.start; if(pre.end > cur.end) cur.end = pre.end; } } for(int i = 0; i < n; i++) { // cout << valid[i] << endl; if(valid[i] == true) res.push_back(intervals[i]); } return res; } };