Problem Description
Given a sorted list of disjoint intervals (each represented as [a, b) where a ≤ x < b), remove the numbers that fall into a given interval (toBeRemoved). The result should be the remaining parts of the original intervals as a sorted list of disjoint intervals.
Key Insights
- Iterate over each interval and check if it overlaps with the toBeRemoved interval.
- If the current interval is completely outside of toBeRemoved, add it to the result as is.
- For overlapping intervals, compute the non-overlapping portions:
- If the left side of the interval lies before toBeRemoved, include [start, min(end, toBeRemoved.start)].
- If the right side of the interval lies after toBeRemoved, include [max(start, toBeRemoved.end), end].
Space and Time Complexity
Time Complexity: O(n), where n is the number of intervals. Space Complexity: O(n), for storing the resulting intervals.
Solution
The approach involves iterating through the list of sorted disjoint intervals and checking for overlap with the toBeRemoved interval. For each interval:
- If it is completely before or after the removal interval, add it directly to the result.
- If it overlaps with toBeRemoved, determine the remaining portion(s):
- The left side of the interval that does not intersect (if any).
- The right side of the interval that does not intersect (if any). Use simple conditional checks and list manipulation to form the final set of intervals.