Ctrl K

Mixed Paint Stripes

Medium
An artist paints a long, thin strip that is easiest to picture as a number line. They made several strokes: strokes[i] = [start, end, color] means the half-open interval [start, end) (including start, excluding end) got paint number color. All paint numbers are distinct. Where strokes overlap, the paints blend, and the colour of a stretch is the set of every paint lying on it. Instead of the set itself, we record the sum of the paint numbers in it. Describe the finished strip with the fewest non-overlapping half-open pieces [left, right, mix], where mix is the sum of the paint numbers on [left, right). Neighbouring pieces must stay separate if their sets of paints differ, even when the sums match. Leave unpainted stretches out of the answer. The pieces may be returned in any order.
Example 1
strokes=[[2, 6, 4], [4, 9, 10]] → [[2, 4, 4], [4, 6, 14], [6, 9, 10]]

Only paint 4 covers [2, 4), paints 4 and 10 mix on [4, 6) (sum 14), and only 10 is left on [6, 9).

Example 2
strokes=[[1, 3, 7], [5, 8, 2]] → [[1, 3, 7], [5, 8, 2]]

Nothing is painted between 3 and 5, so that part is left out of the answer.

Example 3
strokes=[[1, 5, 3], [1, 5, 8], [5, 9, 11]] → [[1, 5, 11], [5, 9, 11]]

Both pieces sum to 11, but the sets of paints differ ({3, 8} and {11}), so they cannot be merged into a single [1, 9).

Constraints
  • 1 ≤ len(strokes) ≤ 2·10⁴
  • len(strokes[i]) == 3
  • 0 ≤ start < end ≤ 10⁵
  • 1 ≤ color ≤ 10⁹
  • All color values are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFlowers in Bloom on Visit Day HardAnagram Buckets MediumMore Than a Third of the Votes MediumStudios for Recording Sessions MediumLongest Run with a Given Sum Medium
loading Python…
Input
strokes=[[2, 6, 4], [4, 9, 10]]
Expected
[[2, 4, 4], [4, 6, 14], [6, 9, 10]]

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