0
0
DSA Pythonprogramming

Non Overlapping Intervals Minimum Removal in DSA Python

Choose your learning style9 modes available
Mental Model
We want to remove the fewest intervals so that no intervals overlap. We do this by always keeping the interval that ends earliest to leave room for others.
Analogy: Imagine scheduling meetings in one room. To fit the most meetings without clashes, always pick the meeting that finishes earliest, then schedule the next meeting that starts after it ends.
Intervals: [1,3] -> [2,4] -> [3,5] -> null
Goal: Remove minimum intervals so none overlap.
Dry Run Walkthrough
Input: intervals: [[1,3], [2,4], [3,5]]
Goal: Remove the fewest intervals so that remaining intervals do not overlap
Step 1: Sort intervals by end time
[1,3] -> [2,4] -> [3,5]
Why: Sorting by end time helps pick intervals that finish earliest first
Step 2: Keep first interval [1,3], set end = 3
[1,3] ↑ -> [2,4] -> [3,5]
Why: Start with earliest finishing interval to maximize room for others
Step 3: Check next interval [2,4], it starts before end=3, so it overlaps; remove it
[1,3] ↑ -> [2,4](removed) -> [3,5]
Why: Overlap means we must remove this interval to avoid clash
Step 4: Check next interval [3,5], it starts at end=3, no overlap; keep it and update end=5
[1,3] -> [3,5] ↑
Why: No overlap, so we keep this interval and update end pointer
Result:
[1,3] -> [3,5] -> null
Minimum removals = 1
Annotated Code
DSA Python
from typing import List

class Solution:
    def eraseOverlapIntervals(self, intervals: List[List[int]]) -> int:
        if not intervals:
            return 0
        # Sort intervals by their end time
        intervals.sort(key=lambda x: x[1])
        count = 0
        end = intervals[0][1]
        for i in range(1, len(intervals)):
            # If current interval starts before end, it overlaps
            if intervals[i][0] < end:
                count += 1  # Remove this interval
            else:
                end = intervals[i][1]  # Update end to current interval's end
        return count

# Driver code
intervals = [[1,3], [2,4], [3,5]]
sol = Solution()
print(sol.eraseOverlapIntervals(intervals))
intervals.sort(key=lambda x: x[1])
Sort intervals by their end time to pick earliest finishing intervals first
end = intervals[0][1]
Initialize end pointer to end of first interval
if intervals[i][0] < end:
Check if current interval overlaps with last kept interval
count += 1
Increment removal count for overlapping interval
else: end = intervals[i][1]
Update end pointer when interval does not overlap
OutputSuccess
1
Complexity Analysis
Time: O(n log n) because we sort the intervals and then scan them once
Space: O(1) because sorting is in-place and only a few variables are used
vs Alternative: Naive approach checks all pairs for overlap leading to O(n^2) time, this greedy method is much faster
Edge Cases
empty intervals list
returns 0 because no intervals to remove
DSA Python
if not intervals:
    return 0
all intervals non-overlapping
returns 0 because no removals needed
DSA Python
if intervals[i][0] < end:
all intervals overlapping
removes all but one interval
DSA Python
count += 1
When to Use This Pattern
When asked to remove minimum intervals to avoid overlaps, use a greedy approach by sorting intervals by end time and removing overlapping ones.
Common Mistakes
Mistake: Sorting intervals by start time instead of end time
Fix: Sort intervals by their end time to ensure earliest finishing intervals are chosen first
Mistake: Not updating the end pointer after keeping an interval
Fix: Update end to current interval's end when interval does not overlap
Summary
Removes the fewest intervals so that no intervals overlap.
Use when you want to schedule intervals without clashes by removing minimum intervals.
Always pick intervals that finish earliest to leave room for others.