TRT
Sign in
mediumAdvancedLeetCode#earliest end first

Non-overlapping intervals

Find the fewest intervals to remove so the rest don't overlap, by keeping as many as possible with the earliest-end rule.

The Runtime Theory Team1 min read
Taught inScheduling and intervalsPro — members onlyTo fit the most non-overlapping intervals, always take the one that ends first. A 'greedy stays ahead' proof shows why, and the same rule solves a family of removal, arrow and partition problems.
Solve it

Solving happens on the judge — come back and mark it done

Sample cases

inintervals = [[1, 2], [2, 3], [3, 4], [1, 3]]

out1

inintervals = [[1, 2], [1, 2], [1, 2]]

out2

inintervals = [[1, 2], [2, 3]]

out0

Return the minimum number of intervals to remove so that the remaining intervals don't overlap. Intervals that only touch, such as [1, 2] and [2, 3], don't overlap.

Before you code

Removing as few as possible is keeping as many as possible: interval scheduling.

Hints

  1. Sort by end.
  2. Keep an interval when its start is at least the last kept end; otherwise count it as removed.
  3. O(n log n).

Production & complexity notes

When solving Non-overlapping intervals on LeetCode (medium), always state your loop or recursion invariant before writing code. Verify how your chosen data structure behaves on degenerate inputs (single-element inputs, all-duplicate keys, strictly monotonic sequences, or maximum constraint sizes up to $10^5$ elements). In production interviews, pair your implementation with an explicit time and auxiliary space complexity proof, explain why alternative brute-force approaches violate the latency budget, and trace how cache locality and memory allocation overhead affect wall-clock throughput on real hardware. Review the step-by-step memory trace in Scheduling and intervals to reinforce the underlying pattern.

What it practices

The earliest-end greedy, from Scheduling and intervals.

Not started

Sign in to save your learning progress.

Sign in to save