Solution: Non-overlapping Intervals
Let's solve the Non-overlapping Intervals problem using the Merge Intervals pattern.
We'll cover the following...
Statement
Given an array of intervals where intervals[i]
contains the
Note: Two intervals
and are considered overlapping if there exists a value such that and . In other words, if there is any point within both intervals (excluding their starting points) where both intervals have values, they are considered overlapping. For example, the intervals and are overlapping, whereas the intervals and are non-overlapping.
Constraints:
intervals.length
intervals[i].length
Solution
Here are the steps of the algorithm:
Sort the
intervals
array in an ascending order based on the end time of each interval.Declare two variables that will assist us in the algorithm:
end
: This stores the end time of the last included interval.remove
: This stores the number of intervals to be removed. It is initialized to. ...