Bird
0
0
DSA Cprogramming~10 mins

Non Overlapping Intervals Minimum Removal in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Non Overlapping Intervals Minimum Removal
Sort intervals by end time
Initialize count=0, prev_end = -∞
For each interval
Check if current start >= prev_end?
NoRemove interval, count++
Yes
Update prev_end = current end
Repeat for all intervals
Return count
Sort intervals by their end time, then iterate to count how many intervals must be removed to avoid overlaps.
Execution Sample
DSA C
int cmp(const void *a, const void *b) {
  return ((int (*)[2])a)[0][1] - ((int (*)[2])b)[0][1];
}
int eraseOverlapIntervals(int intervals[][2], int n) {
  if (n <= 1) return 0;
  qsort(intervals, n, sizeof(intervals[0]), cmp);
  int count = 0, prev_end = intervals[0][1];
  for (int i = 1; i < n; i++) {
    if (intervals[i][0] < prev_end) count++;
    else prev_end = intervals[i][1];
  }
  return count;
}
This code sorts intervals by end time and counts minimum removals to avoid overlaps.
Execution Table
StepOperationCurrent IntervalPrev EndOverlap?CountVisual State
1Sort intervals[1,2], [2,3], [1,3], [3,4]-No0[1,2] -> [2,3] -> [1,3] -> [3,4]
2Initialize[1,2]2No0[1,2] -> [2,3] -> [1,3] -> [3,4]
3Check interval[2,3]2No0[1,2] -> [2,3] -> [1,3] -> [3,4]
4Update prev_end[2,3]3No0[1,2] -> [2,3] -> [1,3] -> [3,4]
5Check interval[1,3]3Yes1[1,2] -> [2,3] -> [1,3] (removed) -> [3,4]
6Check interval[3,4]3No1[1,2] -> [2,3] -> [1,3] (removed) -> [3,4]
7Update prev_end[3,4]4No1[1,2] -> [2,3] -> [3,4]
8End-4-1[1,2] -> [2,3] -> [3,4]
💡 All intervals checked; total removals = 1
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
count00111
prev_end-2344
Key Moments - 3 Insights
Why do we update prev_end only when no overlap is found?
Because prev_end tracks the end of the last interval kept. If overlap occurs, we remove the current interval and do not update prev_end, as the previous interval remains.
Why do we sort intervals by their end time?
Sorting by end time ensures we always keep intervals that finish earliest, maximizing space for future intervals and minimizing removals, as shown in Step 1 of execution_table.
What happens if intervals have the same end time?
They are processed in order after sorting. Overlaps are detected by comparing start with prev_end, so intervals with same end time but overlapping starts will cause removals.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 7, what is the count value?
A2
B0
C1
D3
💡 Hint
Check the 'Count' column at Step 7 in execution_table.
At which step does prev_end update to 3?
AStep 4
BStep 6
CStep 5
DStep 3
💡 Hint
Look at the 'prev_end' column changes in execution_table rows.
If the first interval was [1,5] instead of [1,2], how would count change?
AIt would stay the same
BIt would increase
CIt would decrease
DCannot determine
💡 Hint
Consider how a longer first interval affects overlaps and removals in the execution_table.
Concept Snapshot
Non Overlapping Intervals Minimum Removal:
- Sort intervals by end time
- Initialize count=0, prev_end = first interval end
- For each interval, if start < prev_end, increment count (remove)
- Else update prev_end
- Return count as minimum removals
Full Transcript
To find the minimum number of intervals to remove so that the rest do not overlap, first sort intervals by their end times. Initialize a count of removals to zero and set prev_end to the end of the first interval. Iterate through each interval starting from the second. If the current interval's start is less than prev_end, it overlaps, so increment count and do not update prev_end. Otherwise, update prev_end to the current interval's end. After checking all intervals, the count represents the minimum removals needed.