FastPrepMerge Overlapping Intervals

Merge Overlapping Intervals

FurtherAI logoFurtherAI● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given a non-empty array of closed intervals, merge every pair of intervals that overlaps and return the non-overlapping merged intervals in ascending start order.

Intervals that touch at one endpoint overlap. For example, [1,4] and [4,5] merge into [1,5].

Function

mergeIntervals(intervals: int[][]) → int[][]

Examples

Example 1

intervals = [[1,3],[2,6],[8,10],[15,18]]return = [[1,6],[8,10],[15,18]]

The first two intervals overlap and merge; the other intervals remain separate.

Example 2

intervals = [[1,4],[4,5]]return = [[1,5]]

Closed intervals that share endpoint 4 overlap.

Constraints

  • 1 <= intervals.length <= 10^5.
  • intervals[i].length == 2.
  • -10^9 <= intervals[i][0] <= intervals[i][1] <= 10^9.

More FurtherAI problems

See FurtherAI hiring insights
public int[][] mergeIntervals(int[][] intervals) {
    // Write your code here.
}
intervals[[1,3],[2,6],[8,10],[15,18]]
expected[[1,6],[8,10],[15,18]]
Checking account…