-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsert Interval
More file actions
23 lines (23 loc) · 860 Bytes
/
Insert Interval
File metadata and controls
23 lines (23 loc) · 860 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>>ans;
for(auto &&i:intervals){
// the new interval is after the range of other interval
if(i[1]<newInterval[0])ans.push_back(i);
// the new interval's range is before the other
else if(newInterval[1]<i[0]){
ans.push_back(newInterval);
newInterval=i;//updating the new interval
}
// the new interval is in the range of the other interval
else{
newInterval[0]=min(newInterval[0],i[0]);
newInterval[1]=max(newInterval[1],i[1]);
}
}
//At the end after the loop just add the updated newIntervals
ans.push_back(newInterval);
return ans;
}
};