-
Notifications
You must be signed in to change notification settings - Fork 0
/
insert-interval.cpp
38 lines (38 loc) · 1.11 KB
/
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
33
34
35
36
37
38
/**
* 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;
Interval absorber = newInterval;
for(Interval &ab : intervals){
if (isIntersect(ab, absorber)){
absorber = coalesce(ab, absorber);
}
else{
result.push_back(ab);
}
}
int i = 0, n = result.size();
while (i < n && absorber.start > result[i].end) i++;
result.insert(result.begin() + i, absorber);
return result;
}
bool isIntersect(Interval &ab, Interval &cd){
if (ab.end < cd.start)
return false;
if (cd.end < ab.start)
return false;
return true;
}
Interval coalesce(Interval &ab, Interval &cd){
return Interval(min(ab.start, cd.start), max(ab.end, cd.end));
}
};