Interval List Union

Asked at GoogleOnsite

Reported by the problem's creator, not verified

You work with lists of closed intervals [start, end] with integer endpoints and start <= end. Two intervals belong together when they share at least one point, so [1,3] and [3,5] merge into [1,5], while [1,2] and [3,4] share no point and stay apart even though 2 and 3 are consecutive integers.

The union of interval lists is the smallest set of intervals that covers exactly the same points, written in increasing order of start, with no two of them sharing a point.

Implement union_intervals(lists) to return this union.

Examples

  1. Example 1

    Input: lists = [[[1,3],[8,10],[15,18]],[[2,6],[9,12],[18,20]]]

    Output: [[1,6],[8,12],[15,20]]

    Explanation: [1,3] and [2,6] overlap and become [1,6]; [8,10] and [9,12] become [8,12]; [15,18] and [18,20] share the point 18 and become [15,20].

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.