Non Overlapping Intervals Minimum Removal in DSA Typescript - Time & Space Complexity
We want to find out how the time needed to remove overlapping intervals grows as the number of intervals increases.
How does the code handle sorting and checking intervals as input size changes?
Analyze the time complexity of the following code snippet.
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);
let count = 0;
let end = -Infinity;
for (const interval of 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 overlap and need removal.
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 where n is number of intervals.
- Secondary operation: Single loop through all intervals once (n times) to count overlaps.
Sorting grows faster than simple looping as input grows.
| 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) ≈ 9965 operations for sorting + 1000 for loop |
Pattern observation: Sorting dominates and grows faster than the simple loop as input size increases.
Time Complexity: O(n log n)
This means the time needed grows a bit faster than the number of intervals because of sorting, but the checking loop grows linearly.
[X] Wrong: "The loop checking overlaps is the slowest part, so time complexity is O(n)."
[OK] Correct: Sorting the intervals takes more time than the loop, so it controls the overall speed.
Understanding how sorting affects time helps you explain your solution clearly and shows you know what part of your code matters most as input grows.
"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 intervals do not share any common points except possibly touching ends.Final Answer:
To remove the minimum number of intervals so that the remaining intervals do not overlap -> Option AQuick Check:
Minimum removal = non-overlapping intervals [OK]
- Thinking the goal is to merge intervals
- Confusing maximum overlap with minimum removal
- Sorting intervals without removing any
Solution
Step 1: Identify input and output types
The input is an array of intervals, each interval is an array of two numbers. The output is the count of intervals removed, a number.Step 2: Match the correct signature
function eraseOverlapIntervals(intervals: number[][]): number matches input as number[][] and output as number, which fits the problem.Final Answer:
function eraseOverlapIntervals(intervals: number[][]): number -> Option CQuick Check:
Input array of number arrays, output number [OK]
- Using wrong input type like number[] instead of number[][]
- Returning array instead of number
- Using void return type
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[1] - b[1]);
let count = 0;
let end = -Infinity;
for (const interval of intervals) {
if (interval[0] >= end) {
end = interval[1];
} else {
count++;
}
}
return count;
}
console.log(eraseOverlapIntervals([[1,2],[2,3],[3,4],[1,3]]));Solution
Step 1: Sort intervals by end time
Sorted intervals: [1,2], [2,3], [1,3], [3,4]Step 2: Iterate and count removals
Start with end = -Infinity
Check [1,2]: 1 >= -Infinity true, end=2
[2,3]: 2 >= 2 true, end=3
[1,3]: 1 >= 3 false, count=1
[3,4]: 3 >= 3 true, end=4Final Answer:
1 -> Option AQuick Check:
One interval removed to avoid overlap [OK]
- Not sorting intervals by end time
- Counting intervals incorrectly when start equals end
- Confusing count of removals with count of intervals kept
function eraseOverlapIntervals(intervals: number[][]): number {
intervals.sort((a, b) => a[0] - b[0]);
let count = 0;
let end = intervals[0][1];
for (let i = 1; i < intervals.length; i++) {
if (intervals[i][0] < end) {
count++;
} else {
end = intervals[i][1];
}
}
return count;
}Solution
Step 1: Check sorting criteria
The function sorts intervals by start time, but the greedy approach requires sorting by end time to minimize removals.Step 2: Understand impact on removal count
Sorting by start time can cause incorrect counting of overlapping intervals, leading to wrong removal count.Final Answer:
Sorting intervals by start time instead of end time causes incorrect removals -> Option BQuick Check:
Sort by end time for correct minimum removal [OK]
- Sorting by start time instead of end time
- Wrong initialization of end variable
- Incorrect loop start index
[[0,2],[1,3],[2,4],[3,5],[4,6]], what is the minimum number of intervals to remove to make the rest non-overlapping?Solution
Step 1: Sort intervals by end time
Sorted intervals: [0,2], [1,3], [2,4], [3,5], [4,6]Step 2: Select intervals greedily and count removals
Start with end = -Infinity, count = 0
[0,2]: 0 >= -Infinity true, end=2
[1,3]: 1 < 2 overlap, count=1
[2,4]: 2 >= 2 true, end=4
[3,5]: 3 < 4 overlap, count=2
[4,6]: 4 >= 4 true, end=6Final Answer:
2 -> Option DQuick Check:
Two intervals removed to avoid overlap [OK]
- Not sorting intervals before processing
- Counting intervals kept instead of removed
- Misunderstanding overlap conditions
Ready to prove it?
Pass the test and this pattern moves unknown
