Ctrl K

Unpainted Stretches of a Fence

Medium
A fence consists of length boards numbered 0 to length - 1. Painters went over it several times: each element of coats is a pair [from, to] meaning every board from from to to inclusive got painted. The strokes may overlap and come in any order. Find all unpainted stretches: each one is a maximal run of consecutive bare boards that cannot be extended to the left or right. Return them as pairs [first, last] sorted by increasing first. If no board is bare, return an empty list.
Example 1
length=12, coats=[[2, 4], [7, 9], [3, 5]] → [[0, 1], [6, 6], [10, 11]]

Boards 2–5 and 7–9 are painted. Boards 0–1, 6 and 10–11 stay bare.

Example 2
length=4, coats=[[0, 1], [2, 3]] → []

The two strokes together cover the whole fence, so no board is bare.

Example 3
length=5, coats=[] → [[0, 4]]
Constraints
  • 1 ≤ length ≤ 10⁹
  • 0 ≤ len(coats) ≤ 10⁶
  • 0 ≤ from ≤ to ≤ length − 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumDistinct Orderings With Repeats MediumAnagram Buckets Medium
loading Python…
Input
length=12, coats=[[2, 4], [7, 9], [3, 5]]
Expected
[[0, 1], [6, 6], [10, 11]]

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