CodePark/ problems / Fewest Ranges to Drop
START PRACTICINGPRACTICE
Coding problem · Data structures and algorithms › Sorting and intervals

Fewest Ranges to Drop

mediumabout 25 minutes in an interview

You get a list of ranges [start, end], with start < end. You want to keep ranges so that no two kept ranges overlap. Return how many ranges you have to throw away, using as few as possible.

  • Sharing only an end point is fine. [1, 3] and [3, 5] can both be kept.
  • The ranges are not sorted, and the same range can appear more than once.
  • An empty list gives 0. The list can hold up to 100,000 ranges.
Input: intervals = [[1, 4], [2, 3], [3, 6], [5, 7]] Output: 2Keep [2, 3] and [3, 6], or [2, 3] and [5, 7]. No set of three ranges avoids every overlap.
Input: intervals = [[1, 3], [3, 5], [5, 8]] Output: 0
Input: intervals = [[2, 4], [2, 4], [2, 4]] Output: 2