Filter Occupied Intervals Problem
Description
LeetCode Problem 3975.
You are given a 2D integer array occupiedIntervals, where occupiedIntervals[i] = [start_i, end_i] represents a time interval during which you are occupied. Each interval starts at start_i and ends at end_i, inclusive. These intervals may overlap.
You are also given two integers freeStart and freeEnd, which define a free time interval from freeStart to freeEnd, inclusive.
Your task is to merge all occupied intervals that overlap or touch, then remove all integer points in the free interval from the merged occupied intervals.
Two intervals touch if the second interval starts immediately after the first one ends. For example, [1, 1] and [2, 2] touch and should be merged into [1, 2].
Return the remaining occupied intervals in sorted order. The returned intervals must be non-overlapping and must contain the minimum number of intervals possible. If there are no remaining occupied points, return an empty list.
Example 1:
Input: occupiedIntervals = [[2,6],[4,8],[10,10],[10,12],[14,16]], freeStart = 7, freeEnd = 11
Output: [[2,6],[12,12],[14,16]]
Explanation:
- After merging, the occupied intervals are [2, 8], [10, 12], and [14, 16].
- Excluding the free interval [7, 11] results in [2, 6], [12, 12], and [14, 16].
Example 2:
Input: occupiedIntervals = [[1,5],[2,3]], freeStart = 3, freeEnd = 8
Output: [[1,2]]
Explanation:
- After merging, the occupied interval is [1, 5].
- Excluding the free interval [3, 8] results in [1, 2].
Constraints:
1 <= occupiedIntervals.length <= 5 * 10^4occupiedIntervals[i].length == 21 <= start_i <= end_i <= 10^91 <= freeStart <= freeEnd <= 10^9
Solution
Approach: Merge, Then Cut Out the Free Interval
There are two steps.
Merge. Sort the intervals by start. Walk through them while keeping a list of merged intervals. If the current interval starts after the last merged interval's end plus 1, there is a real gap, so it begins a new merged interval. Otherwise they overlap or touch, and the last interval's end becomes the larger of the two ends. The "plus 1" is what makes [1, 1] and [2, 2] merge into [1, 2].
Cut. For each merged interval [s, e]:
- If it ends before
freeStartor starts afterfreeEnd, it does not meet the free interval, so keep it whole. - Otherwise keep the part before the free interval,
[s, freeStart - 1], ifs < freeStart, and the part after it,[freeEnd + 1, e], ife > freeEnd. An interval inside the free interval disappears.
The merged intervals are sorted and disjoint, so the pieces come out sorted and already minimal in number.
Complexity: O(n log n) time for the sort and O(n) space for the result, where n is the number of occupied intervals.
class Solution:
def filterOccupiedIntervals(
self, occupiedIntervals: List[List[int]], freeStart: int, freeEnd: int
) -> List[List[int]]:
occupiedIntervals.sort(key=lambda x: x[0])
busy = [occupiedIntervals[0]]
for interval in occupiedIntervals[1:]:
if busy[-1][1] + 1 < interval[0]:
busy.append(interval)
else:
busy[-1][1] = max(busy[-1][1], interval[1])
ans = []
for interval in busy:
if interval[1] < freeStart or freeEnd < interval[0]:
ans.append(interval)
else:
if interval[0] < freeStart:
ans.append([interval[0], freeStart - 1])
if interval[1] > freeEnd:
ans.append([freeEnd + 1, interval[1]])
return ans