Intersect Two Sorted Interval Lists
Problem statement
Two arrays, firstList and secondList, contain closed integer intervals. Intervals in each array are sorted by start and do not overlap one another.
Return the nonempty intersections between intervals from different arrays, ordered by start. A shared endpoint forms a one-point intersection: [1,3] and [3,5] intersect at [3,3]. If either array is empty, return an empty array.
Function
intervalIntersection(firstList: int[][], secondList: int[][]) → int[][]Examples
Example 1
firstList = [[0,2],[5,10],[13,23],[24,25]]secondList = [[1,5],[8,12],[15,24],[25,26]]return = [[1,2],[5,5],[8,10],[15,23],[24,24],[25,25]]Several ranges cross, including two intersections consisting of just one shared endpoint.
Example 2
firstList = [[1,3],[5,9]]secondList = []return = []An empty second list has no pair to intersect.
Example 3
firstList = [[2,7]]secondList = [[4,5]]return = [[4,5]]The second interval lies entirely inside the first.
Constraints
0 <= firstList.length, secondList.length <= 1000; their combined length is at least 1.- Each interval is
[start,end]with0 <= start < end <= 1000000000. - Within either array, intervals are sorted and consecutive intervals satisfy
previousEnd < nextStart.