-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathInsert_Interval.cpp
More file actions
24 lines (22 loc) · 833 Bytes
/
Insert_Interval.cpp
File metadata and controls
24 lines (22 loc) · 833 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
class Solution {
public:
vector<Interval> insert(vector<Interval> &intervals, Interval newInterval) {
vector<Interval> result;
vector<Interval>::iterator it;
for (it = intervals.begin(); it != intervals.end(); it++) {
if(newInterval.start < (*it).start) {
intervals.insert(it, newInterval);
break;
}
}
if (it == intervals.end()) {
intervals.insert(it, newInterval);
}
result.push_back(*intervals.begin());
for (it = intervals.begin() + 1; it != intervals.end(); it++) {
if ((*it).start > result.back().end) result.push_back(*it);
else result.back().end = max(result.back().end, (*it).end);
}
return result;
}
};