#435

Non-overlapping Intervals

Medium Sort by end (activity selection)

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the minimum number of intervals to remove so the rest don't overlap (touching ends are fine).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: remove the fewest intervals so none overlap.

Why this works: keep the interval that ends earliest; it leaves the most room for the rest.

Example: [[1,2],[2,3],[3,4],[1,3]] sorted by end

keep [1,2], [2,3], skip [1,3], keep [3,4]removed 1

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Sorting by start and keeping the first — a long early interval can block many short ones.

Solve on LeetCode ↗
Complexity
  • Time: O(n log n).
  • Space: O(log n).
Topic: Greedy
🎬 Episodes 69–73: video coming soon