-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert Interval.h
More file actions
38 lines (34 loc) · 1.12 KB
/
Insert Interval.h
File metadata and controls
38 lines (34 loc) · 1.12 KB
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> merged;
bool isInserted = false;
for(int i = 0; i < intervals.size(); ++i) {
Interval intv = intervals[i];
if(isOverlapped(newInterval, intv)) {
newInterval.start = min(newInterval.start, intv.start);
newInterval.end = max(newInterval.end, intv.end);
} else {
if(!isInserted && intv.start > newInterval.end) {
merged.push_back(newInterval);
isInserted = true;
}
merged.push_back(intv);
}
}
if(!isInserted) merged.push_back(newInterval);
return merged;
}
bool isOverlapped(Interval & i1, Interval & i2) {
return !((i1.end < i2.start) || (i2.end < i1.start));
}
};