To solve the problem of finding the minimum number of intervals that need to be removed to eliminate overlapping, we can use a greedy algorithm approach. This approach is optimal for this specific problem and involves sorting the intervals by their end times and iteratively selecting non-overlapping intervals.
Algorithm
-
Sort the Intervals:
- Sort the intervals based on their end times (endi). If two intervals have the same end time, you can sort by start time as a secondary criterion, but this is not crucial for correctness.
-
Select Non-Overlapping Intervals:
- Initialize a variable,
prev_end, to keep track of the end time of the last added non-overlapping interval. Initialize it with negative infinity (-inf) to allow the first interval to be selected initially.
- Initialize a counter,
count, to keep track of the number of non-overlapping intervals you can include.
-
Iterate Through Sorted Intervals:
- For each interval
[starti, endi] in the sorted list:
- If
starti is greater than or equal to prev_end, it means this interval does not overlap with the last selected interval. You can include it, so update prev_end to endi and increment count.
-
Calculate the Result:
- The minimum number of intervals to remove is the total number of intervals minus the count of non-overlapping intervals (
count).
Proof of Correctness
-
Sorting Rule: By sorting the intervals by their end time, the algorithm ensures that we are always considering the interval that frees up the most time soonest for subsequent intervals to fit in. This is a crucial element of the greedy strategy which prioritizes choice of intervals that leave the most room for other intervals.
-
Greedy Choice Property: The interval sorted by the earliest end time has the greatest potential to allow other intervals to fit after it without overlap. Therefore, choosing this interval can maximize the number of remaining eligible intervals.
-
Optimal Substructure: The problem can be split into subproblems, where choosing a non-overlapping interval sequence from a list of intervals also gives optimal solutions to sublists of intervals. This allows greedy algorithm to be effective due to its recursive optimal substructure.
-
Complexity: Sorting takes (O(n \log n)), and iterating through the intervals takes (O(n)), making the overall time complexity (O(n \log n)).
Here is a Python implementation of this algorithm:
def eraseOverlapIntervals(intervals):
# Sort intervals based on their end time
intervals.sort(key=lambda x: x[1])
# Initialize variables to keep track of previous interval end and count of non-overlapping intervals
prev_end = float('-inf')
count = 0
# Iterate over sorted intervals
for interval in intervals:
start, end = interval
# If the current interval starts after the end of the last included interval, select it
if start >= prev_end:
prev_end = end
count += 1
# The minimum number of intervals to remove
return len(intervals) - count
# Example usage
print(eraseOverlapIntervals([[1, 2], [2, 3], [3, 4], [1, 3]])) # Output: 1
print(eraseOverlapIntervals([[1, 2], [1, 2], [1, 2]])) # Output: 2
This algorithm efficiently removes the minimum number of intervals to ensure that the remaining intervals do not overlap.