Merge Intervals

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

The problem

  • medium
  • Code

Write merge(intervals): intervals is an array of [start, end] pairs in any order. Return them merged so that none overlap, sorted by start. Intervals that only touch count as overlapping, so [[1, 3], [2, 6], [8, 10], [10, 12]] gives [[1, 6], [8, 12]]. Don't change the array you're given or the pairs in it.

Your code must pass

  • Merges overlapping intervals
  • Merges intervals that only touch
  • Sorts intervals given in any order
  • Leaves the input unchanged