forked from a-r-nida/HactoberFest2020-Beginers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Insert Interval.cpp
27 lines (24 loc) · 873 Bytes
/
Insert Interval.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
27
//https://leetcode.com/problems/insert-interval/
//This is the link to the problem you can go and see the problem from there
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int n = intervals.size();
vector<vector<int> > ans;
for(int i = 0; i < n; ++i){
if(intervals[i][1] < newInterval[0]){
ans.push_back(intervals[i]);
}
else if(newInterval[1] < intervals[i][0]){
ans.push_back(newInterval);
newInterval = intervals[i];
}
else{
newInterval[0] = min(newInterval[0],intervals[i][0]);
newInterval[1] = max(newInterval[1],intervals[i][1]);
}
}
ans.push_back(newInterval);
return ans;
}
};