Table of contents
Problem Description :
You are given an array of non-overlapping intervals intervals
where intervals[i] = [start<sub>i</sub>, end<sub>i</sub>]
represent the start and the end of the i<sup>th</sup>
interval and intervals
is sorted in ascending order by start<sub>i</sub>
. You are also given an interval newInterval = [start, end]
that represents the start and end of another interval.
Insert newInterval
into intervals
such that intervals
is still sorted in ascending order by start<sub>i</sub>
and intervals
still does not have any overlapping intervals (merge overlapping intervals if necessary).
Return intervals
after the insertion.
Example 1:
Input: intervals = [[1,3],[6,9]], newInterval = [2,5]
Output: [[1,5],[6,9]]
Explanation of the approach for the problem :
The question gives us an array that contains non-overlapping intervals. Also, the interval array is sorted in ascending order. Every interval contains two elements i.e. one start and one end element. Now we are also given a newInterval array which also contains a start and an end element. Now the question requires us to insert the newInterval in the intervals.
Now there can be three cases where we can insert the values in the intervals array.
Case 1:
When the first value of the newIntervals is greater than the second value of the interval . Then we cannot insert that value in that particular. In that case, what we can do is simply take up that interval in our final answer.
if(newInterval[0] > intervals[curr][1]){ // inserting that interval in the final answer finalAnswer.push_back(intervals[i] ; }
Case 2 :
Now if the first value of the current interval is greater than the second value of the newInterval then we know that we can no longer insert the newInterval in the intervals. This is because our interval array is already sorted and we therefore cannot insert a newElement that is outside the boundary of the end element of the current interval. What we can do is that we simply break out of the loop :
if(intervals[curr][0] > newInterval[1]){ break ; }
Case 3 :
In this condition, we have overlapping values and therefore we can insert the values from the newInterval in the interval array. Now lets take an example to understand this case :
intervals = [[1,3],[6,9]], newInterval = [2,5]
Here the first element in the newInterval is less than the second value of the current interval in the intervals array ( 2 < 3 ). Therefore we can insert this value in the current interval. While merging we need to take the minimum of the first values of both intervals.
Also, the maximum values of the second value of both intervals need to be taken so that the intervals get merged properly.
if(newInterval[0] < intervals[curr][1]){ newInterval[0] = min(newInterval[0] ,intervals[curr][0] ); newInterval[1] = max(newInterval[1] , intervals[curr][1]) ; }
Final Solution for the problem :
class Solution {
public:
vector<vector<int>> insert(vector<vector<int>>& intervals, vector<int>& newInterval) {
int n = intervals.size() ;
vector<vector<int>> finalAnswer;
int curr = 0 ;
while(curr < n ){
if(newInterval[0] > intervals[curr][1] ){
finalAnswer.push_back(intervals[curr]) ;
}
else if(intervals[curr][0] > newInterval[1]){
break ;
}
else{
newInterval[0] = min(newInterval[0] ,intervals[curr][0] );
newInterval[1] = max(newInterval[1] , intervals[curr][1]) ;
}
i++ ;
}
finalAnswer.push_back(newInterval) ;
// after all the insertion of the intervals is done we need to insert the remaining interval into the final answer
while(i < n){
finalAnswer.push_back(intervals[i]) ;
i++ ;
}
return finalAnswer;
}
};
Thanks for visiting my blog. See you next time.