-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0057_Insert_Interval.cpp
More file actions
30 lines (30 loc) · 1007 Bytes
/
0057_Insert_Interval.cpp
File metadata and controls
30 lines (30 loc) · 1007 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
#pragma GCC optimize("Ofast","inline","ffast-math","unroll-loops","no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native","f16c")
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
vector<vector<int>> res;
int i=0;
int n= intervals.size();
while(i<n){
if(intervals[i][1]<newInterval[0]){
res.push_back(intervals[i]);
}
else if(intervals[i][0]>newInterval[1]){
break;
}
else{
newInterval[0]=min(newInterval[0], intervals[i][0]);
newInterval[1]=max(newInterval[1], intervals[i][1]);
}
i++;
}
res.push_back(newInterval);
while(i<n){
res.push_back(intervals[i]);
i++;
}
return res;
}
};