-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path57.cpp
More file actions
19 lines (19 loc) · 661 Bytes
/
57.cpp
File metadata and controls
19 lines (19 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> res;
int idx= 0;
int n = intervals.size();
while(idx<n && intervals[idx][1]<newInterval[0])
res.push_back(intervals[idx++]);
while(idx<n && intervals[idx][0]<=newInterval[1]){
newInterval[0] = min(newInterval[0], intervals[idx][0]);
newInterval[1] = max(newInterval[1], intervals[idx][1]);
idx++;
}
res.push_back(newInterval);
while(idx<n)
res.push_back(intervals[idx++]);
return res;
}
};