Non-overlapping Intervals
On this page
Problem statement
Given a list of intervals
[start, end], return the minimum number of intervals to remove so that the remaining ones don't overlap. Intervals that only touch, such as[4, 6]and[6, 8], do not overlap.
This is the classic activity selection problem, and it is often asked the other way round: "one meeting room, a list of meeting requests: how many meetings can you schedule?" Keeping the most is the same as removing the fewest. Original problem: LeetCode 435.
Example
![Keeping [1,3], [4,6] and [6,8] and removing the two red meetings leaves no overlaps.](https://storage.ghost.io/c/cc/3d/cc3dd2ec-1125-4515-87dd-11389c0d3c50/content/images/2026/11/000-41d590d3-19.png)
intervals = [[1,3], [2,5], [4,6], [6,8], [5,9]]
Output: 2 # remove [2,5] and [5,9]; 3 meetings fit in one room
intervals = [[1,2], [2,3]]
Output: 0 # touching is fine
Constraints (assumed)
- 0 ≤ n ≤ 10⁵; −5 × 10⁴ ≤ start < end ≤ 5 × 10⁴.
What this question tests
- Knowing (and justifying) the right greedy choice: earliest end first.
- Seeing that "fewest removed" and "most kept" are the same problem.
- The touching endpoints rule:
≥versus>.
Questions to ask first
- Do touching intervals overlap? (Here: no.)
- Return the number removed, or the number kept? (Here: removed; "max meetings" asks for kept.)
- Is the input sorted? (Here: no.)
- Do you need which intervals to remove? (Here: no; it is easy to add.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.