Loading...
Loading...
You are given two lists of integers starts and ends of the same length n. Together they describe n intervals: interval i covers every point from starts[i] to ends[i], including both ends.
Two intervals meet if they share at least one point. Intervals that only touch at an endpoint, like [1, 3] and [3, 5], also meet.
A group of intervals is anchored if at least one interval in the group meets every other interval in the group. A group with only one interval is always anchored.
Return the minimum number of intervals you need to remove so that the remaining intervals form an anchored group.
Input: starts = [1,3,4,6,9], ends = [2,8,5,7,10]
Output: 2
Explanation: The intervals are [1, 2], [3, 8], [4, 5], [6, 7] and [9, 10].
[3, 8] meets [4, 5] and [6, 7], but not [1, 2] or [9, 10].
Removing [1, 2] and [9, 10] leaves an anchored group with [3, 8] meeting the other two.
Every other interval misses at least three of the rest, so 2 is the minimum.
Input: starts = [1,2,3,4], ends = [2,3,4,5]
Output: 1
Explanation: [2, 3] touches [1, 2] at 2 and [3, 4] at 3, so it meets both.
It misses only [4, 5]. Removing [4, 5] leaves an anchored group.
No interval meets all three of the others, so at least one removal is needed.
Input: starts = [1,5,9], ends = [2,6,10]
Output: 2
Explanation: No two of the intervals meet, so only a single interval can remain.
Input: starts = [1,2,3], ends = [9,4,5]
Output: 0
Explanation: [1, 9] meets both [2, 4] and [3, 5], so the group is already anchored.
starts.length ≤2⋅105ends.length = starts.lengthstarts[i] ≤ ends[i] ≤109