-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert Interval
27 lines (27 loc) · 959 Bytes
/
Insert Interval
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
class Solution {
public:
static bool comparator(vector<int>& a,vector<int>& b){
return a<b;
}
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
intervals.push_back(newInterval);
sort(intervals.begin(),intervals.end(),comparator);
vector<vector<int>> ans;
for(int i=0;i<intervals.size();i++){
if(i<intervals.size()-1 && intervals[i][1]>=intervals[i+1][0]){
int a=intervals[i][0],b=max(intervals[i][1],intervals[i+1][1]);
int j=i+1;
while(j<intervals.size()-1 && b>=intervals[j+1][0]){
b=max(b,intervals[j+1][1]);
j++;
}
// b=max(b,intervals[j][1]);
ans.push_back({intervals[i][0],b});
i=j;
}else{
ans.push_back(intervals[i]);
}
}
return ans;
}
};