Non-overlapping Intervals

Medium#435sort by end

You get a list of intervals. Remove as few of them as possible so that no two of the rest overlap, and return how many you removed. Intervals that only touch, like [1, 3] and [3, 5], do not overlap. Our example: [4, 7], [0, 9], [8, 11], [1, 3], [10, 12], [3, 5], [6, 8] — the answer is 3.

input
intervals: intervals [start, end] in any order; they may overlap, touch or repeat.
returns
The fewest intervals to remove so that no two of the rest overlap. Intervals that only touch, like [1, 3] and [3, 5], do not overlap.
constraints
  • 1 ≤ intervals.length ≤ 10⁴
  • −5·10⁴ ≤ startᵢ < endᵢ ≤ 5·10⁴
our 11 tests keep to them
examples
Open a test in the tests panel: it shows the input and the expected answer.
/**
 * @param {number[][]} intervals
 * @return {number}
 */
function eraseOverlapIntervals(intervals) {

}
Ctrl ↵
format
reset

saved in this browser

11 tests · limit 3 s

Stuck?

See the solution run step by step on our example, then why it works and its complexity.

Watch the walkthrough →

Next: more intervals problems

Pattern “Intervals” →

The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.