forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert-interval.cpp
26 lines (23 loc) · 896 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
// Time: O(n)
// Space: O(1)
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
size_t i = 0;
vector<vector<int>> result;
// Insert intervals appeared before newInterval.
while (i < size(intervals) && newInterval[0] > intervals[i][1]) {
result.emplace_back(intervals[i++]);
}
// Merge intervals that overlap with newInterval.
while (i < size(intervals) && newInterval[1] >= intervals[i][0]) {
newInterval = {min(newInterval[0], intervals[i][0]),
max(newInterval[1], intervals[i][1])};
++i;
}
result.emplace_back(newInterval);
// Insert intervals appearing after newInterval.
copy(cbegin(intervals) + i, cend(intervals), back_inserter(result));
return result;
}
};