FastPrepMerge Intervals

Merge Intervals

Lime logoLime● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given an array of closed intervals where intervals[i] = [start_i, end_i], merge every pair of overlapping intervals.

Return the non-overlapping intervals covering the same values, sorted by start time. Intervals that share an endpoint are considered overlapping.

Function

merge(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 into [1,6].

Example 2

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

Closed intervals sharing endpoint 4 overlap.

Example 3

intervals = [[8,10],[1,4],[2,3],[6,9],[3,7]]return = [[1,10]]

After sorting, the intervals form one connected overlap chain.

Constraints

  • 1 <= intervals.length <= 10000.
  • intervals[i].length == 2.
  • 0 <= start_i <= end_i <= 100000.
See Lime hiring insights
public int[][] merge(int[][] intervals) {
    // Write your code here.
}
intervals[[1,3],[2,6],[8,10],[15,18]]
expected[[1,6],[8,10],[15,18]]
Checking account…