Skip to content
AI360Xpert
Beta
Difficulty: MediumIntervals

Non-overlapping Intervals

Problem in Plain English

You are given a list of intervals and may delete any of them. Return the smallest number of deletions that leaves the remaining intervals pairwise non-overlapping.

Problem Statement

Given a nonempty list intervals of positive-length intervals [start, end], remove the fewest intervals so the remaining ones are pairwise non-overlapping. Return the removal count, not the retained intervals.

Intervals sharing only an endpoint are compatible: [1,2] and [2,3] may both remain. Input order is arbitrary and endpoints may be negative. The implementations below preserve the original outer order and all row values.

Constraints

  • 1≤n=intervals.length≤1051 \leq n = \mathrm{intervals.length} \leq 10^5.
  • Every interval contains exactly two integer endpoints.
  • −5×104≤starti<endi≤5×104-5 \times 10^4 \leq \mathrm{start}_i < \mathrm{end}_i \leq 5 \times 10^4.

Examples

Example 1

Input
intervals = [[1,2],[2,3],[3,4],[1,3]]
Output
1

Remove [1,3] and the remaining [1,2], [2,3], [3,4] are pairwise non-overlapping.

Example 2

Input
intervals = [[1,2],[1,2],[1,2]]
Output
2

Keep any one copy of [1,2] and remove the other two.

Example 3

Input
intervals = [[1,2],[2,3]]
Output
0

The intervals only touch at 2, so nothing needs removing.

Example 4

Input
intervals = [[0,10],[1,2],[2,3],[3,4]]
Output
1

Remove the long interval [0,10] and keep all three short intervals. Keeping the earliest-starting interval instead would force three removals.

Example 5

Input
intervals = [[-5,-1]]
Output
0

A single interval never conflicts with another. Negative endpoint values do not change the compatibility test.

Intuition

Minimizing removals is equivalent to maximizing how many intervals we keep. In Example 1, keeping [1,2], [2,3], and [3,4] beats keeping the longer [1,3] with [3,4]. An interval ending sooner leaves at least as much room for every future choice. That suggests sorting by end and accepting compatible intervals, while exhaustive keep-or-drop choices provide a direct baseline for small inputs. Endpoint contact is allowed here, unlike closed-interval merging.

Approaches

Exhaustive Subsets

Solution Details

Reveal Exhaustive Subsets: intuition, complexity, and code

Hints

Hint 1
Removing the fewest intervals is the same as keeping the most intervals.
Hint 2
An interval that ends early can only ever leave more room for what follows.
Hint 3
Sort by end time, keep an interval whenever its start clears the last kept end, and count everything else as removed.

Edge Cases

  • All identical intervals, such as three copies of [1,2], need all but one removed.
  • Already non-overlapping input like [[1,2],[2,3]] returns 0.
  • Touching endpoints never count as overlap: [1,2] and [2,3] coexist.

Common Mistakes and Interview Tips

  • Keeping the earliest-starting overlapping interval can block many short ones: [[0,10],[1,2],[2,3],[3,4]] needs only one removal. Prefer earliest end.
  • Using start > lastEnd wrongly removes touching intervals. The acceptance test is start >= lastEnd.
  • Updating lastEnd when removing an interval destroys the meaning of the last retained end and can cause unnecessary removals.
  • Enumerating masks with 1 << n overflows or wraps in Java and JavaScript at large n. The Boolean-array counter has no bit-width limit, though exhaustive search still takes exponential time.
  • Sorting the argument in place changes its ordering. Both approaches sort only an outer copy and never edit borrowed rows.

Key Takeaway

To keep the most intervals, always keep the one that ends first: sorting by end time makes that greedy choice available at every step.