Non Overlapping Intervals Minimum Removal in DSA C# - Time & Space Complexity
We want to find out how long it takes to decide the minimum number of intervals to remove so that no intervals overlap.
How does the time needed grow as we have more intervals?
Analyze the time complexity of the following code snippet.
public int EraseOverlapIntervals(int[][] intervals) {
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1]));
int count = 0, end = int.MinValue;
foreach (var interval in intervals) {
if (interval[0] >= end) {
end = interval[1];
} else {
count++;
}
}
return count;
}
This code sorts intervals by their end time, then counts how many intervals must be removed to avoid overlaps.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting the intervals array.
- How many times: Sorting takes multiple comparisons, roughly proportional to n log n for n intervals.
- Secondary operation: One loop through all intervals to count removals, runs n times.
Sorting grows faster than the loop as input size increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 * log(10) ≈ 33 operations for sorting + 10 for loop |
| 100 | About 100 * log(100) ≈ 664 operations for sorting + 100 for loop |
| 1000 | About 1000 * log(1000) ≈ 9966 operations for sorting + 1000 for loop |
Pattern observation: The sorting step dominates and grows faster than the simple loop.
Time Complexity: O(n log n)
This means as the number of intervals grows, the time needed grows a bit faster than just the number of intervals, mainly due to sorting.
[X] Wrong: "The loop that counts removals is the slowest part, so time is O(n)."
[OK] Correct: Sorting takes more time than the loop, so it controls the overall speed.
Understanding this time complexity helps you explain how sorting affects performance and why greedy choices after sorting are efficient.
"What if the intervals were already sorted by end time? How would the time complexity change?"
Practice
Non Overlapping Intervals Minimum Removal problem?Solution
Step 1: Understand the problem goal
The problem asks to remove as few intervals as possible so that no two intervals overlap.Step 2: Clarify what non-overlapping means
Non-overlapping means for any two intervals, one ends before the other starts.Final Answer:
To remove the minimum number of intervals so that the remaining intervals do not overlap -> Option BQuick Check:
Minimum removal = no overlaps [OK]
- Thinking the goal is to merge intervals
- Confusing maximum overlap with minimum removal
- Sorting intervals without removing any
Solution
Step 1: Identify sorting criteria for minimum removal
We sort intervals by their end time ascending to greedily keep intervals that finish earliest.Step 2: Match code to sorting by end time ascending
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1])); uses a[1].CompareTo(b[1]) which sorts by end time ascending correctly.Final Answer:
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1])); -> Option DQuick Check:
Sort by end time ascending = Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1])); [OK]
- Sorting by start time instead of end time
- Sorting in descending order by mistake
- Using wrong array indices for comparison
int[][] intervals = new int[][] {
new int[] {1, 3},
new int[] {2, 4},
new int[] {3, 5}
};
Array.Sort(intervals, (a, b) => a[1].CompareTo(b[1]));
int count = 0;
int end = intervals[0][1];
for (int i = 1; i < intervals.Length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
Console.WriteLine(count);Solution
Step 1: Sort intervals by end time
Sorted intervals by end: [1,3], [2,4], [3,5]Step 2: Count overlaps by comparing start with current end
Check intervals: - i=1: start=2 < end=3 -> overlap, count=1 - i=2: start=3 < end=3? No, update end=5Final Answer:
1 -> Option AQuick Check:
One overlap counted = 1 [OK]
- Not updating end when no overlap
- Counting intervals that do not overlap
- Sorting incorrectly before loop
int[][] intervals = new int[][] {
new int[] {1, 2},
new int[] {1, 3},
new int[] {2, 4}
};
Array.Sort(intervals, (a, b) => a[0].CompareTo(b[0]));
int count = 0;
int end = intervals[0][1];
for (int i = 1; i < intervals.Length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
Console.WriteLine(count);Solution
Step 1: Check sorting criteria
The code sorts intervals by start time, but greedy approach requires sorting by end time.Step 2: Understand impact on counting removals
Sorting by start time can cause wrong overlaps detection and incorrect count.Final Answer:
Sorting by start time instead of end time causes incorrect removal count -> Option AQuick Check:
Sort by end time needed, not start time [OK]
- Sorting by start time instead of end time
- Starting loop at wrong index
- Confusing interval start and end in comparisons
[[1,2],[2,3],[3,4],[1,3]], what is the minimum number of intervals to remove to make the rest non-overlapping?Solution
Step 1: Sort intervals by end time ascending
Sorted intervals: [1,2], [1,3], [2,3], [3,4]Step 2: Count overlaps and removals
Start with end=2 (from [1,2]) - [1,3]: start=1 < end=2 overlap, count=1 - [2,3]: start=2 <= end=2 no overlap, update end=3 - [3,4]: start=3 <= end=3 no overlap, update end=4 Total removals = 1Step 3: Re-check for minimal removals
Check if removing [1,3] is enough. Yes, only 1 removal needed.Final Answer:
1 -> Option CQuick Check:
Remove overlapping [1,3] only = 1 [OK]
- Counting all overlapping intervals instead of minimal removals
- Not sorting intervals before processing
- Confusing intervals with same start or end times
Ready to prove it?
Pass the test and this pattern moves unknown
