Skip to content

3323. Minimize Connected Groups by Inserting Interval 👍

  • Time: $O(\texttt{sort})$
  • Space: $O(n)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
class Solution {
 public:
  int minConnectedGroups(vector<vector<int>>& intervals, int k) {
    int mergedIntervals = 0;
    int maxMergedIntervals = 0;

    intervals = merge(intervals);

    int i = 0;
    for (const vector<int>& interval : intervals) {
      const int end = interval[1];
      while (i < intervals.size() && end + k >= intervals[i][0]) {
        ++mergedIntervals;
        ++i;
      }
      --mergedIntervals;  // Exclude intervals[i].
      maxMergedIntervals = max(maxMergedIntervals, mergedIntervals);
    }

    return intervals.size() - maxMergedIntervals;
  }

 private:
  // Same as 56. Merge Intervals
  vector<vector<int>> merge(vector<vector<int>>& intervals) {
    vector<vector<int>> res;
    ranges::sort(intervals);
    for (const vector<int>& interval : intervals)
      if (res.empty() || res.back()[1] < interval[0])
        res.push_back(interval);
      else
        res.back()[1] = max(res.back()[1], interval[1]);
    return res;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
  public int minConnectedGroups(int[][] intervals, int k) {
    int mergedIntervals = 0;
    int maxMergedIntervals = 0;

    intervals = merge(intervals);

    int i = 0;
    for (int[] interval : intervals) {
      final int end = interval[1];
      while (i < intervals.length && end + k >= intervals[i][0]) {
        ++mergedIntervals;
        ++i;
      }
      --mergedIntervals; // Exclude intervals[i].
      maxMergedIntervals = Math.max(maxMergedIntervals, mergedIntervals);
    }

    return intervals.length - maxMergedIntervals;
  }

  // Same as 56. Merge Intervals
  public int[][] merge(int[][] intervals) {
    List<int[]> res = new ArrayList<>();
    Arrays.sort(intervals, Comparator.comparingInt((int[] interval) -> interval[0]));
    for (int[] interval : intervals)
      if (res.isEmpty() || res.get(res.size() - 1)[1] < interval[0])
        res.add(interval);
      else
        res.get(res.size() - 1)[1] = Math.max(res.get(res.size() - 1)[1], interval[1]);
    return res.toArray(int[][] ::new);
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution:
  def minConnectedGroups(self, intervals: list[list[int]], k: int) -> int:
    mergedIntervals = 0
    maxMergedIntervals = 0

    intervals = self._merge(intervals)

    i = 0
    for _, end in intervals:
      while i < len(intervals) and end + k >= intervals[i][0]:
        mergedIntervals += 1
        i += 1
      mergedIntervals -= 1  # Exclude intervals[i].
      maxMergedIntervals = max(maxMergedIntervals, mergedIntervals)

    return len(intervals) - maxMergedIntervals

  # Same as 56. Merge Intervals
  def _merge(self, intervals: list[list[int]]) -> list[list[int]]:
    res = []
    for interval in sorted(intervals):
      if not res or res[-1][1] < interval[0]:
        res.append(interval)
      else:
        res[-1][1] = max(res[-1][1], interval[1])
    return res