-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathinsert_interval.cpp
32 lines (25 loc) · 1002 Bytes
/
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
30
31
32
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
if(newInterval.empty()) return intervals;
vector<vector<int>> result;
for(auto interval : intervals) {
//before the range
if(newInterval[1] < interval[0]) {
result.push_back(newInterval);
newInterval = interval;
}
//after the range
else if(newInterval[0] > interval[1]) {
result.push_back(interval);
}
//overlap .. min start, max end
else if(newInterval[1] > interval[0] || newInterval[0] < interval[1]) {
newInterval[0] = min(newInterval[0], interval[0]);
newInterval[1] = max(newInterval[1], interval[1]);
}
}
result.push_back(newInterval);
return result;
}
};