Skip to content

Non-overlapping Intervals

MediumIntervals
Decode it first. Clues, constraints, pattern and plan before any code.

Return the minimum number of intervals to remove so that the rest do not overlap. Intervals that only touch, like [1, 2] and [2, 3], do not overlap.

[[1, 2], [2, 3], [3, 4], [1, 3]]  →  1   (remove [1, 3])
[[1, 2], [1, 2], [1, 2]]          →  2
[[1, 2], [2, 3]]                  →  0

Constraints

  • 0 <= intervals.length <= 10^5
  • -5 * 10^4 <= start < end <= 5 * 10^4