FastPrepMerge Intervals
Problem · Array

Merge Intervals

Learn this problem
MediumGoogle logoGoogleINTERNPHONE SCREEN
See Google hiring insights

Problem statement

Given an array of closed integer intervals intervals, where each interval is [start, end], merge every pair of overlapping intervals.

Closed intervals that touch at one endpoint overlap. Return disjoint intervals that represent the same union, sorted by increasing start value.

Function

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

Examples

Example 1

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

Intervals [1,3] and [2,6] overlap. Intervals [8,10] and [10,18] touch at endpoint 10, so they also merge.

Example 2

intervals = []return = []

An empty input has an empty union.

Constraints

  • 0 <= intervals.length <= 200000
  • Every row of intervals has exactly two integers [start, end].
  • -10^9 <= start <= end <= 10^9

More Google problems

drafts saved locally
public int[][] mergeIntervals(int[][] intervals) {
    // write your code here
}
intervals[[1,3],[2,6],[8,10],[10,18]]
expected[[1,6],[8,18]]
checking account