-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57.cpp
More file actions
32 lines (29 loc) · 782 Bytes
/
Copy path57.cpp
File metadata and controls
32 lines (29 loc) · 782 Bytes
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
/**
* 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) {
auto is = intervals;
int i;
for (i = is.size()-1; i >= 0; i--) {
if (is[i].start <= newInterval.start) break;
}
is.insert(is.begin()+i+1, newInterval);
for (i = 1; i < is.size();) {
if (is[i-1].end >= is[i].start) {
is[i-1].end = max(is[i-1].end, is[i].end);
is.erase(is.begin()+i);
} else {
i++;
}
}
return is;
}
};