Non-overlapping Intervals

Difficulty: Medium

You're given a list of intervals, some overlapping. Return the fewest intervals you must remove so the rest don't overlap.

Here, intervals that only touch (one ends where the other starts) do not overlap. Both can stay.

Examples

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

Removing [1,3] leaves [[1,2],[2,3],[3,4]], which only touch at shared endpoints, so nothing overlaps.

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

All three intervals are identical and fully overlap, so two of the three must go, keeping just one.

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

These only touch at the point 2, which doesn't count as overlapping, so nothing needs to be removed.

Constraints

  • 1 <= intervals.length <= 10^5

  • intervals[i].length == 2

  • -5 * 10^4 <= start < end <= 5 * 10^4

Approach

One option is dynamic programming. Sort by start time, and for each interval find the longest chain of non-overlapping intervals ending there, checking every earlier interval that doesn't conflict. The answer is the total minus the longest chain. It's correct but checks many pairs a better order would skip.

The faster route is a classic greedy trick: sort by end time. Walk through, tracking the end of the last interval you kept:

  • If the next interval starts before that end, it overlaps, so remove it. Never remove the kept one: it ends earliest, so it's the safer one to keep.
  • Otherwise, keep it and update the "last kept end" to its end.

Solutions

Brute Force — DP on Longest Non-overlapping Chain

Sort by start time. dp[i] is the longest chain of non-overlapping intervals ending with interval i. To compute it, check every earlier interval j that ends at or before i starts, since both can be kept.

The largest value in dp is the most you can keep, so remove the total minus that maximum.

function eraseOverlapIntervals(intervals) {
  const n = intervals.length;
  if (n === 0) return 0;

  const sorted = [...intervals].sort((a, b) => a[0] - b[0]);
  const dp = new Array(n).fill(1);
  let maxKept = 1;

  for (let i = 1; i < n; i++) {
    for (let j = 0; j < i; j++) {
      if (sorted[j][1] <= sorted[i][0]) {
        dp[i] = Math.max(dp[i], dp[j] + 1);
      }
    }
    maxKept = Math.max(maxKept, dp[i]);
  }

  return n - maxKept;
}

Time: O(n^2) — for each interval, scan every earlier interval · Space: O(n) for the dp array

Optimal — Greedy, Sort by End Time

Sort by end time and walk left to right, tracking the end of the last kept interval. Always keep the first one, since it ends earliest.

  • If the next interval starts at or after the tracked end, keep it and update the end.
  • If it starts before, it overlaps, so remove it. Leave the tracked end alone: the kept interval ends earlier and is the better anchor.
function eraseOverlapIntervals(intervals) {
  if (intervals.length === 0) return 0;

  const sorted = [...intervals].sort((a, b) => a[1] - b[1]);
  let lastEnd = sorted[0][1];
  let keptCount = 1;

  for (let i = 1; i < sorted.length; i++) {
    if (sorted[i][0] >= lastEnd) {
      keptCount++;
      lastEnd = sorted[i][1];
    }
  }

  return sorted.length - keptCount;
}

Time: O(n log n) — dominated by the sort · Space: O(n) for the sorted copy