Non-overlapping Intervals

Find the minimum number of intervals to remove for no overlaps.

Pattern

Greedy Interval Scheduling

This problem follows the Greedy Interval Scheduling pattern, commonly found in the Intervals category. Recognizing this pattern is key to solving it efficiently in an interview setting.

Approach

How to Solve It

Sort by end time. Greedily keep intervals that don't overlap. Count removals.

Key Insight

Sort by end time, not start — keeping intervals that end earliest maximizes room for future intervals. This is the classic interval scheduling greedy.

Step-by-step

  1. 1Sort intervals by end time (greedy choice)
  2. 2Track the end of the last kept interval
  3. 3For each interval: if it starts before the last end, it overlaps — remove it
  4. 4Otherwise, keep it and update the end

Pseudocode

intervals.sort(key=lambda x: x[1])
count = 0
end = -infinity
for start, e in intervals:
    if start >= end:
        end = e  # keep this interval
    else:
        count += 1  # remove this interval
return count
Complexity Analysis

Time Complexity

O(n log n)

Space Complexity

O(1)
More Intervals Problems

Master this pattern with YeetCode

Practice Non-overlapping Intervals and similar Intervals problems with flashcards. Build pattern recognition through active recall.

Practice this problem