Practice · Intervals · 2 of 4

Fewest Ranges to Remove

You are given a list of ranges intervals, each [start, end] with start < end. Return the smallest number of ranges you must remove so that no two of the remaining ranges overlap.

Here, ranges that only touch at an end point do not overlap: [1, 3] and [3, 5] can both stay.

Example 1

Input
intervals = [[1, 4], [2, 3], [3, 6], [5, 7]]
Output
2
Remove [1, 4] and [5, 7]. [2, 3] and [3, 6] only touch, so both can stay.

Example 2

Input
intervals = [[0, 5], [0, 5], [0, 5]]
Output
2
Copies of a range overlap each other, so only one can stay.

Constraints

  • intervals holds 1 to 10⁵ ranges.
  • −5 × 10⁴ ≤ start < end ≤ 5 × 10⁴