-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathInsert Interval.cpp
29 lines (29 loc) · 923 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
28
29
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
vector<vector<int>> output;
int n = intervals.size();
int i = 0;
while(i < n){
if(newInterval[1] < intervals[i][0]){
output.push_back(newInterval);
while(i < n){
output.push_back(intervals[i]);
i++;
}
return output;
}
else if(newInterval[0] > intervals[i][1]){
output.push_back(intervals[i]);
i++;
}
else{
newInterval[0] = min(newInterval[0], intervals[i][0]);
newInterval[1] = max(newInterval[1], intervals[i][1]);
i++;
}
}
output.push_back(newInterval);//think about it
return output;
}
};