-
Notifications
You must be signed in to change notification settings - Fork 3
/
57.cpp
26 lines (23 loc) · 787 Bytes
/
57.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
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int n = intervals.size();
int index = 0;
vector<vector<int>> res;
while (index < n && intervals[index][1] < newInterval[0]) {
res.push_back(intervals[index]);
index++;
}
while (index < n && intervals[index][0] <= newInterval[1]) {
newInterval[0] = min(intervals[index][0], newInterval[0]);
newInterval[1] = max(intervals[index][1], newInterval[1]);
index++;
}
res.push_back(newInterval);
while (index < n) {
res.push_back(intervals[index]);
index++;
}
return res;
}
};