Given A and B, which are two interval lists. A has no overlap inside A and B has no overlap inside B. In A, the intervals are sorted by their starting points. In B, the intervals are sorted by their starting points. How do you merge the two interval lists and output the result with no overlap?
One method is to concatenate the two lists, sort by the starting point, and apply merge intervals as discussed at https://www.geeksforgeeks.org/merging-intervals/. Is there a more efficient method?
Here is an example:
A: [1,5], [10,14], [16,18]
B: [2,6], [8,10], [11,20]
The output:
[1,6], [8, 20]
Here is a different approach, in the spirit of the answer to the question of overlaps.
If both lists are empty - return the empty list. If only one is empty, return the other. If the upper bound of one list is below the lower bound of the other, there is no unification possible, so return the other and proceed with the rest. If they overlap, don't return, but call the method recursively, the unification on the side of the more far reaching interval and without the consumed less far reaching interval.
The union method looks similar to the one which does the overlap:
The test for non overlap is the same. But min and max have changed places.
So for (2, 4) (3, 5) the overlap is (3, 4), the union is (2, 5).
Table of min/max lower/upper.
Now for the tricky or unclear case from above:
0-4 and 5-8 don't overlap, so they form two different results which don't get merged.