Ctrl K

Bridge the Coverage Gaps

Medium
You are given a list of segments segments, where segments[i] = [start, end]. Segments that overlap or touch at an endpoint form one solid block: for example, [1, 3], [3, 6] and [2, 2] together cover [1, 6] with no gaps. But [1, 2] and [3, 4] are two separate blocks, because the stretch between 2 and 3 is not covered. You must add exactly one new segment [a, b] whose length b − a is at most max_len. Return the smallest number of solid blocks you can end up with.
Example 1
segments=[[2, 4], [7, 9], [10, 12], [20, 25]], max_len=3 → 3

Add [4, 7]: [2, 4], [4, 7] and [7, 9] form one block. [10, 12] and [20, 25] remain apart, so there are 3 blocks.

Example 2
segments=[[1, 5], [3, 8]], max_len=2 → 1

The segments already form a single block [1, 8]; the new one can go inside it.

Example 3
segments=[[1, 1], [5, 5], [9, 9]], max_len=8 → 1
Constraints
  • 1 ≤ len(segments) ≤ 10⁵
  • 1 ≤ start ≤ end ≤ 10⁹
  • 0 ≤ max_len ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsNearest k Values MediumEven Out With Increments MediumRug Over Painted Stretches MediumLargest Equal Group After Nudging MediumNudge Values to Agree Medium
loading Python…
Input
segments=[[2, 4], [7, 9], [10, 12], [20, 25]], max_len=3
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 133 tests on the server, hidden ones included.