To solve the problem of finding the minimum number of intervals to remove so that the remaining intervals do not overlap, we can utilize a greedy algorithm approach. This method is both efficient and effective for such interval scheduling problems.
Algorithm Design
Step-by-Step Approach
-
Sort the Intervals:
- Sort the given list of intervals based on their end times in ascending order.
- If two intervals have the same end time, sort them based on their start times.
-
Initialize Variables:
count_non_overlapping = 0: To keep track of the number of non-overlapping intervals selected.
end = -∞: To store the end time of the last selected interval.
-
Iterate Through the Sorted Intervals:
- For each interval
[start, end_i] in the sorted list:
- If
start >= end:
- This interval does not overlap with the last selected interval.
- Select this interval:
- Increment
count_non_overlapping by 1.
- Update
end to end_i.
- Else:
- This interval overlaps with the last selected interval.
- Skip this interval (effectively marking it for removal).
-
Calculate the Minimum Removals:
- The minimum number of intervals to remove is given by:
[
\text{min_removals} = \text{total_intervals} - \text{count_non_overlapping}
]
Pseudocode
def eraseOverlapIntervals(intervals):
if not intervals:
return 0
# Sort intervals based on end time
intervals.sort(key=lambda x: x[1])
count_non_overlapping = 0
end = float('-inf')
for interval in intervals:
if interval[0] >= end:
count_non_overlapping += 1
end = interval[1]
return len(intervals) - count_non_overlapping
Example Execution
Example 1:
Input: intervals = [[1, 2], [2, 3], [3, 4], [1, 3]]
Sorted Intervals by End Time: [[1, 2], [2, 3], [1, 3], [3, 4]]
Selection Process:
- Select [1, 2] → end = 2
- Select [2, 3] → end = 3
- [1, 3] overlaps with [2, 3], skip
- Select [3, 4] → end = 4
Non-overlapping intervals selected: 3
Minimum removals: 4 - 3 = 1
Example 2:
Input: intervals = [[1, 2], [1, 2], [1, 2]]
Sorted Intervals by End Time: [[1, 2], [1, 2], [1, 2]]
Selection Process:
- Select [1, 2] → end = 2
- [1, 2] overlaps with [1, 2], skip
- [1, 2] overlaps with [1, 2], skip
Non-overlapping intervals selected: 1
Minimum removals: 3 - 1 = 2
Proof of Correctness
Greedy Choice Property
The algorithm makes a greedy choice by always selecting the interval with the earliest end time that does not overlap with previously selected intervals. This choice maximizes the number of non-overlapping intervals selected. By selecting the interval that finishes earliest, we leave as much room as possible for subsequent intervals, thereby allowing more intervals to be included without overlap.
Optimal Substructure
Once an interval is selected, the problem reduces to selecting the maximum number of non-overlapping intervals from the remaining intervals that start after the current interval's end time. This exhibits an optimal substructure, meaning that an optimal solution to the problem contains optimal solutions to its subproblems.
No Overlaps
By ensuring that each selected interval starts after or exactly when the previous selected interval ends, the algorithm guarantees that there are no overlapping intervals in the final selection.
Minimizing Removals
Since the algorithm maximizes the number of non-overlapping intervals, the number of intervals to remove (which is the total number minus the selected non-overlapping intervals) is minimized.
Conclusion
The greedy algorithm correctly identifies the maximum number of non-overlapping intervals by always selecting the earliest finishing interval available. This strategy ensures that we leave as much space as possible for future intervals, thereby minimizing the number of intervals that need to be removed to eliminate all overlaps.