FastPrepMerge Overlapping Closed Intervals

Merge Overlapping Closed Intervals

Applied Intuition logoApplied Intuition● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

Given closed integer intervals [start, end], merge every pair that overlaps or touches at an endpoint. Return disjoint intervals sorted by start.

Function

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

Examples

Example 1

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

The first two overlap and the last two touch at 10.

Example 2

intervals = [[5,7]]return = [[5,7]]

One interval is already merged.

Example 3

intervals = [[4,5],[1,10],[2,3]]return = [[1,10]]

The containing interval absorbs both others.

Constraints

  • 1 <= intervals.length <= 10^5.
  • -10^9 <= start <= end <= 10^9.

More Applied Intuition problems

See Applied Intuition hiring insights
public int[][] mergeIntervals(int[][] intervals) {
    // Write your solution here.
}
intervals[[1,3],[2,6],[8,10],[10,12]]
expected[[1,6],[8,12]]
Checking account…