Merge Intervals

Try to solve the Merge Intervals problem.

Statement

We are given an array of closed intervalsclosedintervals, intervals, where each interval has a start time and an end time. The input array is sorted with respect to the start times of each interval. For example, intervals = [ [1,4], [3,6], [7,9] ][~[1,4], ~[3,6], ~[7,9]~] is sorted in terms of start times 1, 31, ~3, and 77.

Your task is to merge the overlapping intervalsOverlapping intervals have at least one time (start/end) in common. and return a new output array consisting of only the non-overlapping intervals.

Constraints:

  • 1≤1 \leq intervals.length ≤103\leq 10^3
  • intervals[i].length =2= 2
  • 0≤0 \leq start time ≤\leq end time ≤104\leq 10^4

Examples

Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy