Describe the Painting

There is a long and thin painting that can be represented by a number line. The painting was painted with multiple overlapping segments where each segment was painted with a unique color.

You are given a 2D integer array segments, where segments[i] = [starti, endi, colori] represents the half-closed segment [starti, endi) with colori as the color.

The colors in overlapping segments were mixed when the painting was made. When two or more colors mix, they form a new color represented as a set of mixed colors. For simplicity, you should output only the sum of the elements in the set rather than the full set.

Describe the finished painting with the minimum number of non-overlapping half-closed segments of these mixed colors. Return a 2D array painting where painting[j] = [leftj, rightj, mixj] describes a half-closed segment [leftj, rightj) with mixed color sum mixj.

Exclude any parts that are not painted. You may return the segments in any order.

A half-closed segment [a, b) is the section of the number line between points a and b, including point a and not including point b.

Example 1
Inputsegments = [[1,4,5],[4,7,7],[1,7,9]]
Output[[1,4,14],[4,7,16]]
The interval [1,4) mixes colors 5 and 9 for sum 14, while [4,7) mixes colors 7 and 9 for sum 16.
Example 2
Inputsegments = [[1,7,9],[6,8,15],[8,10,7]]
Output[[1,6,9],[6,7,24],[7,8,15],[8,10,7]]
The color sums over the painted intervals are 9 on [1,6), 24 on [6,7), 15 on [7,8), and 7 on [8,10).

Constraints

  • 1 <= segments.length <= 2 * 10^4
  • segments[i].length == 3
  • 1 <= starti < endi <= 10^5
  • 1 <= colori <= 10^9
  • Each colori is distinct.

Asked at 3 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate