Given a sorted list of disjoint intervals, each interval intervals[i] = [a, b) represents the set of real numbers x such that a <= x < b.
We make a request to remove all real numbers in the interval toBeRemoved.
Return a sorted list of disjoint intervals after removing all real numbers in toBeRemoved.
You may assume the given input is well-formed in the sense that the intervals are disjoint and sorted:
intervals[i][0] < intervals[i][1]intervals[i][0] < intervals[i+1][0]Example 1:
Input: intervals = [[0,2],[3,4],[5,7]], toBeRemoved = [1,6] Output: [[0,1],[6,7]]
Example 2:
Input: intervals = [[0,5]], toBeRemoved = [2,3] Output: [[0,2],[3,5]]
Example 3:
Input: intervals = [[-5,-4],[-3,-2],[1,2],[3,5],[8,9]], toBeRemoved = [-1,4] Output: [[-5,-4],[-3,-2],[4,5],[8,9]]
Constraints:
1 <= intervals.length <= 104-109 <= intervals[i][0] < intervals[i][1] <= 1090 <= toBeRemoved[0] < toBeRemoved[1] <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to remove an interval involves checking every part of the larger range to see if it overlaps with the interval we want to remove. We'll consider each small piece individually and decide whether to keep it or discard it based on the interval to be removed.
Here's how the algorithm would work step-by-step:
def remove_interval_brute_force(ranges, interval_to_remove):
result = []
for current_range in ranges:
range_start = current_range[0]
range_end = current_range[1]
# Check if the current range is completely to the left of the interval to remove.
if range_end <= interval_to_remove[0]:
result.append(current_range)
# Check if the current range is completely to the right of the interval to remove.
elif range_start >= interval_to_remove[1]:
result.append(current_range)
# Handle cases where the current range overlaps with the interval.
else:
if range_start < interval_to_remove[0]:
result.append([range_start, interval_to_remove[0]])
if range_end > interval_to_remove[1]:
result.append([interval_to_remove[1], range_end])
return resultThe goal is to efficiently remove parts of intervals from a list of intervals. Instead of complex comparisons, we'll check how each interval relates to the removal interval and construct a new list of intervals that represents the result after the removal.
Here's how the algorithm would work step-by-step:
def remove_interval(intervals, interval_to_remove):
resulting_intervals = []
for current_interval in intervals:
# Check if the interval is completely before the interval to remove
if current_interval[1] <= interval_to_remove[0]:
resulting_intervals.append(current_interval)
# Check if the interval is completely after the interval to remove
elif current_interval[0] >= interval_to_remove[1]:
resulting_intervals.append(current_interval)
else:
# Handle overlapping intervals
if current_interval[0] < interval_to_remove[0]:
# Add the portion of the interval before the removed interval
resulting_intervals.append([current_interval[0], interval_to_remove[0]])
if current_interval[1] > interval_to_remove[1]:
# Add the portion of the interval after the removed interval
resulting_intervals.append([interval_to_remove[1], current_interval[1]])
return resulting_intervals| Case | How to Handle |
|---|---|
| intervals or toBeRemoved is null or empty | If intervals is null/empty, return an empty list; if toBeRemoved is null, return intervals. |
| intervals array is not sorted | The problem statement may imply sorted input, but explicitly sort intervals by start time to guarantee correctness. |
| toBeRemoved completely contains all intervals | Return an empty list, as all intervals are removed. |
| toBeRemoved is completely disjoint from all intervals | Return the original intervals array. |
| An interval is completely contained within toBeRemoved | This interval should be removed entirely (not added to the result). |
| Overlapping intervals within intervals array | Handle overlaps correctly by splitting intervals into smaller, non-overlapping intervals according to toBeRemoved. |
| Extreme start and end values for intervals (Integer.MIN_VALUE, Integer.MAX_VALUE) | Ensure the comparison and arithmetic operations involving start and end times do not cause integer overflow. |
| toBeRemoved interval has Integer.MIN_VALUE or Integer.MAX_VALUE as bounds | Account for edge cases related to comparisons against these boundary values without causing exceptions. |