↓Skip to main content
  1. LeetCode/

LeetCode 3323: Minimize Connected Groups by Inserting Interval

·2 mins· ·
LeetCode Medium Sliding-Window Sorting Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-28

  • Submissions: Accepted (first try)

Approach
#

  1. Merge first: sort and merge overlapping or touching intervals into m separate groups.
  2. The new interval has length at most k. To join groups l through r, it must span from the end of group l to the start of group r: merged[r].start - merged[l].end <= k.
  3. A sliding window finds the most groups cnt one interval can join; the answer is m - (cnt - 1).

As r moves right its start grows, so the reachable l only moves right: the condition is monotonic.

My Solution
#

class Solution:
    def minConnectedGroups(self, intervals: List[List[int]], k: int) -> int:
        intervals.sort()
        merged = []
        for start, end in intervals:
            if merged and start <= merged[-1][1]:
                merged[-1][1] = max(merged[-1][1], end)
            else:
                merged.append([start, end])

        best = left = 0
        for right in range(len(merged)):
            while merged[right][0] - merged[left][1] > k:
                left += 1                          # the new interval cannot reach
            best = max(best, right - left + 1)
        return len(merged) - (best - 1)

With right == left, merged[right][0] - merged[left][1] is negative, so left never passes right and best is at least 1.

Complexity
#

Time O(n log n), space O(n).

Mistakes to Avoid
#

  • Sliding over the raw intervals without merging: overlapping intervals would count as several groups.
  • Joining cnt groups removes cnt - 1 groups, not cnt.

Interview Explanation
#

I sort and merge the intervals into separate groups. The new interval is at most k long, so joining groups l through r needs group r’s start minus group l’s end to be at most k. That is monotonic in r, so a sliding window finds the most groups one interval can join, and the group count drops by that number minus one. Time O(n log n).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 3413: Maximum Coins From K Consecutive Bags
·3 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 3413 note: the best k bags always have one end on a segment boundary; compute “left end at a segment start” and “right end at a segment end” (the latter by mirroring and reusing the same function)
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 1040: Moving Stones Until Consecutive II
·3 mins
LeetCode Medium Sliding-Window Math Sorting Array
LeetCode 1040 note: max moves = total gaps minus the edge gap the first move wastes; min moves uses a length-n window holding the most stones, with a special case of 2 when n - 1 stones are already consecutive and one is far away