Interval List Union
GoogleOnsiteMedium
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.
Part 1: Union of two lists
You are given lists, holding exactly two lists of closed intervals. Inside each list the intervals are sorted by start and no two of them share a point. A list can be empty.
Return the union of the two lists: the smallest set of disjoint closed intervals covering exactly the same points, in increasing order of start. If both lists are empty, return an empty list.
- Example 1
- lists
[[[1,3],[8,10],[15,18]],[[2,6],[9,12],[18,20]]]- Output
[[1,6],[8,12],[15,20]]
Why: [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].
Constraints
len(lists) == 20 <= len(lists[0]) + len(lists[1]) <= 10^5-10^9 <= interval[0] <= interval[1] <= 10^9for each intervalintervalinlists