Remove Overlapping Intervals

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code
  • Pro

Write minRemovals(intervals): each interval is a [start, end] pair of whole numbers with start less than end, in any order. Return the fewest intervals to remove so that no two of those left overlap. Intervals that only touch, like [1, 2] and [2, 3], don't overlap. Return 0 for an empty array. Don't change intervals. For example, minRemovals([[1, 2], [2, 3], [3, 4], [1, 3]]) is 1: remove [1, 3]. The checks include 100,000 intervals, so comparing every pair is too slow.

Your code must pass

  • Removes the fewest in small examples
  • Doesn't count touching intervals as overlapping
  • Returns 0 when nothing overlaps
  • Leaves intervals unchanged
  • Is fast enough for 100,000 intervals