Minimum Interval Selection to Cover Target Intervals Problem
Description
LeetCode Problem 4086.
You are given two 2D integer arrays, availableIntervals and targetIntervals, each representing a list of closed intervals.
Select a subset of intervals from availableIntervals so that every real point contained in any interval in targetIntervals belongs to at least one selected interval.
Points outside the target intervals do not need to be covered. Selected intervals may overlap or extend beyond the target intervals.
Return the minimum number of intervals you need to select. If it is impossible to cover all the target intervals, return -1.
Example 1:
Input: availableIntervals = [[1,4],[1,7],[3,9],[5,10],[10,15]], targetIntervals = [[1,3],[2,5],[7,8],[10,13],[11,15],[12,13]]
Output: 3
Explanation:
The target intervals cover the ranges [1, 5], [7, 8], and [10, 15]. By selecting [1, 4], [3, 9], and [10, 15] from availableIntervals, all target intervals can be fully covered with three intervals.
Example 2:
Input: availableIntervals = [[4,6],[1,4],[2,5]], targetIntervals = [[3,4],[2,5],[6,7]]
Output: -1
Explanation:
It is impossible to select intervals from availableIntervals to cover all the intervals in targetIntervals.
Example 3:
Input: availableIntervals = [[1,2],[3,4]], targetIntervals = [[1,4]]
Output: -1
Explanation:
Selecting both available intervals leaves every real point strictly between 2 and 3 uncovered. For example, 2.5 belongs to the target interval but to neither available interval.
Constraints:
1 <= availableIntervals.length, targetIntervals.length <= 5 * 10^4availableIntervals[i].length == targetIntervals[j].length == 21 <= availableIntervals[i][0] <= availableIntervals[i][1] <= 10^51 <= targetIntervals[j][0] <= targetIntervals[j][1] <= 10^5
Solution
Approach: Sort and Greedy Extension
Sort both lists by left endpoint. Keep cur, the rightmost point that is already covered (it starts at 0), and a pointer i into the sorted available intervals.
For each target [l, r], while cur < r there is still an uncovered stretch. Its first uncovered point is uncover = max(l, cur). Among the available intervals whose left end is at most uncover, take the one reaching farthest right, nxt (the pointer only moves forward, because targets are processed in sorted order).
- If
nxt < uncover, no interval contains that point, so return -1. - If
nxt == cur, coverage cannot move right, so return -1. - Otherwise select that interval, set
cur = nxtand add one to the answer.
Closed intervals chain only when they touch or overlap. A gap such as 2 to 3 between [1, 2] and [3, 4] leaves real points uncovered, and the check nxt < uncover reports it.
Complexity: O(n log n + m log m) time for the sorting, then one linear scan; O(log n + log m) extra space for sorting, where n and m are the sizes of the two lists.
class Solution:
def minSelection(
self, availableIntervals: List[List[int]], targetIntervals: List[List[int]]
) -> int:
availableIntervals.sort(key=lambda x: x[0])
targetIntervals.sort(key=lambda x: x[0])
ans = cur = 0
i = 0
for l, r in targetIntervals:
while cur < r:
uncover = max(l, cur)
nxt = cur
while (
i < len(availableIntervals) and availableIntervals[i][0] <= uncover
):
nxt = max(nxt, availableIntervals[i][1])
i += 1
if nxt < uncover or nxt == cur:
return -1
cur = nxt
ans += 1
return ans