|
| 1 | +# Non-overlapping Intervals |
| 2 | + |
| 3 | +Given an array of intervals `intervals` where `intervals[i] = [starti, endi)` contains the half-open interval (start, endi), |
| 4 | +return the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. |
| 5 | + |
| 6 | +> A half-open interval is one that contains only one of its boundary elements. The “(” parenthesis denotes the exclusion |
| 7 | +> of the starting point. The “]” bracket denotes the inclusion of the ending point. |
| 8 | +
|
| 9 | +Note that intervals which only touch at a point are non-overlapping. For example, [1, 2] and [2, 3] are non-overlapping. |
| 10 | + |
| 11 | +> Note: Two intervals (a,b] and (c,d] are considered overlapping if there exists a value x such that a<x≤b and c<x≤d. In |
| 12 | +> other words, if there is any point within both intervals (excluding their starting points) where both intervals have |
| 13 | +> values, they are considered overlapping. For example, the intervals (7,11] and (10,12] are overlapping, whereas the |
| 14 | +> intervals (2,4] and (4,5] are non-overlapping. |
| 15 | +
|
| 16 | +## Examples |
| 17 | + |
| 18 | +Example 1: |
| 19 | +```text |
| 20 | +Input: intervals = [[1,2],[2,3],[3,4],[1,3]] |
| 21 | +Output: 1 |
| 22 | +Explanation: [1,3] can be removed and the rest of the intervals are non-overlapping. |
| 23 | +``` |
| 24 | + |
| 25 | +Example 2: |
| 26 | +```text |
| 27 | +Input: intervals = [[1,2],[1,2],[1,2]] |
| 28 | +Output: 2 |
| 29 | +Explanation: You need to remove two [1,2] to make the rest of the intervals non-overlapping. |
| 30 | +``` |
| 31 | + |
| 32 | +Example 3: |
| 33 | +```text |
| 34 | +Input: intervals = [[1,2],[2,3]] |
| 35 | +Output: 0 |
| 36 | +Explanation: You don't need to remove any of the intervals since they're already non-overlapping. |
| 37 | +``` |
| 38 | + |
| 39 | +## Constraints |
| 40 | + |
| 41 | +- 1 <= intervals.length <= 10^5 |
| 42 | +- intervals[i].length == 2 |
| 43 | +- -5 * 10^4 <= starti < endi <= 5 * 10^4 |
| 44 | + |
| 45 | +## Topics |
| 46 | + |
| 47 | +- Array |
| 48 | +- Dynamic Programming |
| 49 | +- Greedy |
| 50 | +- Sorting |
| 51 | + |
| 52 | +## Solution |
| 53 | + |
| 54 | +1. Sort the intervals array in an ascending order based on the end time of each interval. |
| 55 | +2. Declare two variables that will assist us in the algorithm: |
| 56 | + - end: This stores the end time of the last included interval. |
| 57 | + - remove: This stores the number of intervals to be removed. It is initialized to |
| 58 | +3. Traverse the sorted intervals array to determine which interval needs to be excluded. For each interval, the following |
| 59 | + conditions are checked: |
| 60 | + - If the start time of the current interval is greater than or equal to end, this interval does not overlap with the |
| 61 | + previously included interval and can be included. Therefore, we update end to the end time of the current interval, |
| 62 | + which is the next earliest possible end time. |
| 63 | + - Otherwise, the current interval overlaps with the previously included intervals. Therefore, it must be removed, so |
| 64 | + we increment remove. |
| 65 | +4. After the sorted intervals array has been traversed completely, there are no more intervals left to evaluate, so we |
| 66 | + return remove, which now contains the minimum number of intervals to be removed. |
| 67 | + |
| 68 | +### Time Complexity |
| 69 | + |
| 70 | +The time complexity of this solution is O(n log(n)), where n is the length of the `intervals` array. |
| 71 | + |
| 72 | +Explanation: |
| 73 | + |
| 74 | +- Time taken to sort the intervals array: O(nlog(n)) |
| 75 | +- Time taken to traverse the intervals array: O(n) |
| 76 | + |
| 77 | +Therefore, the overall time complexity becomes O(n + n log(n)), which simplifies to O(nlog(n)). |
| 78 | + |
| 79 | +### Space Complexity |
| 80 | + |
| 81 | +The space complexity of this solution is O(1). |
0 commit comments