You are given a 2D array intervals, where intervals[i] = [starti, endi]
represents the start and the end of interval i. You are also given an integer k.
You must 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. Here are some examples:
A group of intervals [[1, 2], [2, 5], [3, 3]] is connected because together they cover the range from 1 to 5 without any gaps.
However, a group of intervals [[1, 2], [3, 4]] is not connected because the segment (2, 3) is not covered.
Return the minimum number of connected groups after adding exactly one new interval to the array.
Input: intervals =[[1,3],[5,6],[8,10]], k =3Output: 2Explanation:
After adding the interval `[3, 5]`, we have two connected groups:`[[1, 3],
[3, 5], [5, 6]]` and `[[8, 10]]`.
Example 2:
1
2
3
4
5
Input: intervals =[[5,10],[1,1],[3,3]], k =1Output: 3Explanation:
After adding the interval `[1, 1]`, we have three connected groups:`[[1, 1],
[1, 1]]`,`[[3, 3]]`, and `[[5, 10]]`.
To minimize the number of connected groups after inserting a new interval, we should target the largest gaps between existing groups. By sorting intervals and analyzing the gaps, we can optimally place the new interval (of length ≤ k) to cover as many gaps as possible, thus merging groups.
classSolution {
public:int minimizeGroups(vector<vector<int>>& intervals, int k) {
sort(intervals.begin(), intervals.end());
vector<pair<int, int>> merged;
for (auto& it : intervals) {
if (merged.empty() || merged.back().second < it[0])
merged.push_back({it[0], it[1]});
else merged.back().second = max(merged.back().second, it[1]);
}
vector<int> gaps;
for (int i =1; i < merged.size(); ++i)
gaps.push_back(merged[i].first - merged[i-1].second);
sort(gaps.begin(), gaps.end());
int ans = merged.size();
int sum =0;
for (int g : gaps) {
if (sum + g > k) break;
sum += g;
ans--;
}
return ans;
}
};
classSolution {
funminimizeGroups(intervals: Array<IntArray>, k: Int): Int {
intervals.sortBy { it[0] }
val merged = mutableListOf<IntArray>()
for (itin intervals) {
if (merged.isEmpty() || merged.last()[1] < it[0])
merged.add(intArrayOf(it[0], it[1]))
else merged.last()[1] = maxOf(merged.last()[1], it[1])
}
val gaps = mutableListOf<Int>()
for (i in1 until merged.size)
gaps.add(merged[i][0] - merged[i-1][1])
gaps.sort()
var ans = merged.size; var sum = 0for (g in gaps) {
if (sum + g > k) break sum += g
ans-- }
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defminimize_connected_groups(intervals: list[list[int]], k: int) -> int:
intervals.sort()
merged = []
for it in intervals:
ifnot merged or merged[-1][1] < it[0]:
merged.append(it[:])
else:
merged[-1][1] = max(merged[-1][1], it[1])
gaps = [merged[i][0] - merged[i-1][1] for i in range(1, len(merged))]
gaps.sort()
ans, sum_gap = len(merged), 0for g in gaps:
if sum_gap + g > k:
break sum_gap += g
ans -=1return ans
impl Solution {
pubfnminimize_connected_groups(mut intervals: Vec<Vec<i32>>, k: i32) -> i32 {
intervals.sort();
letmut merged =vec![];
for it in intervals {
if merged.is_empty() || merged.last().unwrap()[1] < it[0] {
merged.push(it.clone());
} else {
merged.last_mut().unwrap()[1] = merged.last().unwrap()[1].max(it[1]);
}
}
letmut gaps: Vec<i32>= (1..merged.len()).map(|i| merged[i][0] - merged[i-1][1]).collect();
gaps.sort();
letmut ans = merged.len() asi32;
letmut sum =0;
for g in gaps {
if sum + g > k { break; }
sum += g;
ans -=1;
}
ans
}
}