← Practice · Intervals

Insert Interval Medium

You are given a list of non-overlapping intervals sorted by start, and a new interval. Insert the new interval so the list stays sorted and non-overlapping, merging where needed, and return the result.

Examples

  • intervals = [[1,3],[6,9]], newInterval = [2,5] -> [[1,5],[6,9]]
  • intervals = [], newInterval = [5,7] -> [[5,7]]

Define a function named insert in JavaScript.

00:00▶ Visualize

JavaScript · Tab indents · Ctrl/⌘+Enter runs the tests