i
and j
to 0 for firstList
and secondList
respectively.intersections
to store the intersections.i < len(firstList)
and j < len(secondList)
, do the following:
a. Find the maximum of the start values of the current intervals from firstList
and secondList
.
b. Find the minimum of the end values of the current intervals from firstList
and secondList
.
c. If the maximum start value is less than or equal to the minimum end value, there is an intersection, so add the interval [max(start values), min(end values)]
to intersections
.
d. Increment the pointer i
or j
depending on which interval has the smallest endpoint.intersections
.