616. Minimize Connected Groups by Inserting Interval
Given a 2D array intervals, where intervals[i] = [startᵢ, endᵢ] represents the start and end of the iᵗʰ interval. You are also given an integer k.
The task is to add exactly one new interval [startnew, endnew] to the array such that:
- The length of the new interval, endnew - startnew, is at most k.
- After adding, the number of connected groups in intervals is minimized.
A connected group of intervals is a maximal collection of intervals that, when considered together, cover a continuous range from the smallest point to the largest point with no gaps between them. The set of intervals [[2, 4], [4, 7], [5, 6]] is connected because together they cover the range [2, 7] without gaps. However, the intervals [[2, 4], [6, 8]] are not connected because the segment (4, 6) is not covered.
Return the minimum number of connected groups after adding exactly one new interval.
Example 1:
Input: intervals = [[2, 5], [7, 9], [12, 15]], k = 4Â Â
Output: 2
Explanation: Adding the interval [5, 9] merges the first two groups into one, resulting in two connected group:
[2, 9] and [12, 15]]
Example 2:
Input: intervals = [[3, 7], [10, 12], [14, 18]], k = 1Â Â
Output: 3
Explanation: Adding any interval won't reduce the number of connected groups, hence the answer is 3.
Now Your Turn!
Pick the correct output for the given inputInput: intervals = [[1, 2], [5, 6], [9, 12]], k = 3Â
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ intervals.length ≤ 105
- intervals[i] == [startáµ¢, endáµ¢]
- 1 ≤ startᵢ ≤ endᵢ ≤ 109
- 1 ≤ k ≤ 109