Interval List Intersections

You are given two lists of closed intervals, firstList and secondList. Within each list, the intervals are sorted by start and pairwise disjoint (no two share a point).

Return the intersection of the two lists: every closed interval of points that lies inside some interval of firstList and inside some interval of secondList, sorted by start.

The intersection of two closed intervals is either empty or another closed interval. For example, [1, 5] and [3, 9] intersect in [3, 5], and [1, 5] and [5, 10] intersect in the single point [5, 5].

Constraints

  • 0 <= firstList.length, secondList.length <= 1000
  • 0 <= start <= end <= 10^9
  • Each list is sorted by start and its intervals are disjoint.

Examples

Example 1

Input: firstList = [[0,3],[6,9],[12,15]], secondList = [[2,7],[10,13]]
Output: [[2,3],[6,7],[12,13]]

[0, 3] and [2, 7] share [2, 3]; [6, 9] and [2, 7] share [6, 7]; [12, 15] and [10, 13] share [12, 13].

Example 2

Input: firstList = [[1,4]], secondList = []
Output: []

The second list is empty, so the lists share nothing.

Example 3

Input: firstList = [[1,5]], secondList = [[5,10]]
Output: [[5,5]]

Closed intervals that touch share a single point, written as the interval [5, 5].

JavaScript you'll use: Arrays: create, read, update Destructuring, spread and rest

/**
 * @param {number[][]} firstList
 * @param {number[][]} secondList
 * @return {number[][]}
 */
function intervalIntersection(firstList, secondList) {
  
}

Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.