Non-overlapping Intervals

Medium

Topics
ArrayGreedySortingIntervals

Given an array of intervals (each [start, end]), return the minimum number of intervals you must remove so the rest are non-overlapping.

Example 1

14[1,2][2,3][3,4][1,3]
keptremoved
Maximising what you keep is the same problem as minimising what you remove. That keeps 3, so 1 must go.
Each lane is one interval drawn on a shared axis, so overlaps line up vertically.
Input:  grid = [[1,2],[2,3],[3,4],[1,3]]
Output: 1

Example 2

12[1,2][1,2][1,2]
keptremoved
Input:  grid = [[1,2],[1,2],[1,2]]
Output: 2

Constraints

  • 1 <= intervals.length <= 10^5
  • intervals[i].length == 2
Editorial — intuition, approach, complexity and pitfalls

Intuition

Greedily keep the interval that ends earliest — it leaves the most room for the rest. Sorting by end time and always keeping compatible intervals maximizes how many you keep, minimizing removals.

Approach

  1. Sort by end time.
  2. Track the end of the last kept interval; if the next starts at or after it, keep it and advance the end.
  3. Otherwise it overlaps and must be removed.

Dry Run

intervals [[1,2], [2,3], [3,4], [1,3]]
Sort by END, then keep greedily:
  sorted  [1,2] [2,3] [1,3] [3,4]
  [1,2]   starts at 1, nothing kept yet     KEEP, end = 2   kept 1
  [2,3]   starts at 2 >= 2                  KEEP, end = 3   kept 2
  [1,3]   starts at 1 < 3                   overlaps, remove
  [3,4]   starts at 3 >= 3                  KEEP, end = 4   kept 3
answer 4 - 3 = 1 removal
Counting what to KEEP and subtracting is easier to get right than counting
removals directly, and it turns the problem into the classic
activity-selection greedy.
Sorting by end is what makes the greedy optimal: among all intervals that could be
kept next, the one finishing earliest leaves the most room for everything after
it, and no later choice can beat that. Sorting by start breaks it -- though not on
this input, where it also returns 1. On [[1,100],[2,3],[3,4],[4,5]] it returns 3
instead of 1, because the greedy locks onto the enormous [1,100] first and then
has to discard everything else.
The comparison is `s >= end`, non-strict, so intervals that merely touch are both
kept. That matches the problem: [1,2] and [2,3] do not overlap.

Complexity

  • Time — O(n log n): the sort dominates.
  • Space — O(log n) in Java and C++, O(n) in Python: the greedy scan itself holds two values, so O(1) of its own — but the sort is part of the algorithm and is where the space goes. An in-place quicksort recurses for O(log n) of stack; Python's Timsort allocates an O(n) merge buffer. Sorting by end coordinate is not optional here, so this cost cannot be removed.

Common Pitfalls

  • Sort by end, not start — that is what makes the greedy choice optimal. The page's own input returns 1 either way, so it cannot show this; [[1,100],[2,3],[3,4],[4,5]] can, returning 3 instead of 1 because the greedy locks onto the enormous [1,100] first.

Full Solution — all languages

class Solution:
    def eraseOverlapIntervals(self, grid):
        grid.sort(key=lambda x:x[1])
        end=float('-inf'); kept=0
        for s,e in grid:
            if s>=end:
                kept+=1; end=e
        return len(grid)-kept
Asked at Capital One
SensAI
Beta
Tap the mic to stop voice mode