~/DHRUVUpskilling
← board/DSA/merge Intervel/dsa-merge-intervel-03
Backlog·queued

Interval List Intersections

DifficultyMedium
Patternmerge Intervel
TrackDSA
tl;dr

For two arrays of closed intervals given as input, intervalListA and intervalListB, where each interval has its own start and end time, write a function that returns the intersection of the two interval arrays.

full write-up

Statement

For example, the intersection of [3, 8] and [5, 10] is [5, 8].

Constraints

  • 0 ≤ intervalListA.length, intervalListB.length ≤ 1000
  • 0 ≤ start[i] < end[i] ≤ 10⁹, where i is used to indicate intervalListA
  • end[i] < start[i + 1]
  • 0 ≤ start[j] < end[j] ≤ 10⁹, where j is used to indicate intervalListB
  • end[j] < start[j + 1]

Examples

Example 1

Input:

  • intervalListA = [[1, 4], [5, 8], [9, 12]]
  • intervalListB = [[2, 3], [6, 10]]

Explanation:

  • [1, 4][2, 3] = [2, 3]
  • [5, 8][6, 10] = [6, 8]
  • [9, 12] has no overlap with any interval in intervalListB.

Output: [2, 3], [6, 8]

Example 2

Input:

  • intervalListA = [[3, 8], [10, 15]]
  • intervalListB = [[5, 10], [12, 20]]

Explanation:

  • [3, 8][5, 10] = [5, 8]
  • [10, 15][12, 20] = [12, 15]

Output: [5, 8], [12, 15]