-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path057. Insert Interval.cpp
29 lines (29 loc) · 1007 Bytes
/
057. Insert Interval.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* 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>res;
for(auto x: intervals)
if(x.end >= newInterval.start && x.start <= newInterval.end){
newInterval.start = min(newInterval.start, x.start);
newInterval.end = max(newInterval.end, x.end);
}
for(auto x: intervals)
if(x.end < newInterval.start || x.start > newInterval.end) res.push_back(x);
for(int i = 0; i < res.size(); i++)
if(res[i].start > newInterval.start){
res.insert(res.begin() + i, newInterval);
break;
}
if(res.empty() || res.back().end < newInterval.start) res.push_back(newInterval);
return res;
}
};