Describe the Painting - Problem

Imagine you're an art curator documenting a unique painting technique where artists layer colors on a canvas represented as a number line. Each artist paints a segment with their signature color, and when segments overlap, the colors mix together to create beautiful new hues!

You're given an array segments where segments[i] = [starti, endi, colori] represents a half-closed interval [starti, endi) painted with colori.

Color Mixing Rules:

  • When segments overlap, their colors mix together
  • Mixed colors are represented as sets: {2, 4, 6}
  • For simplicity, we use the sum of colors instead of the full set
  • Example: Colors 2, 4, and 6 mixing = sum of 12

Your Goal: Describe the final painting using the minimum number of non-overlapping segments, where each segment shows the mixed color sum for that region.

Example: If segments = [[1,4,5],[1,7,7]]:
• Region [1,4): Colors {5,7} mix → sum = 12
• Region [4,7): Only color {7} → sum = 7
• Result: [[1,4,12],[4,7,7]]

Input & Output

example_1.py — Basic Overlapping Segments
$ Input: segments = [[1,4,5],[1,7,7]]
Output: [[1,4,12],[4,7,7]]
💡 Note: From position 1 to 4, both segments overlap so colors 5 and 7 mix giving sum 12. From position 4 to 7, only the second segment with color 7 remains.
example_2.py — Multiple Overlapping Regions
$ Input: segments = [[1,7,9],[6,8,15],[8,10,7]]
Output: [[1,6,9],[6,7,24],[7,8,15],[8,10,7]]
💡 Note: Region [1,6): only color 9. Region [6,7): colors 9+15=24. Region [7,8): only color 15. Region [8,10): only color 7.
example_3.py — Non-overlapping Segments
$ Input: segments = [[1,4,5],[6,7,7]]
Output: [[1,4,5],[6,7,7]]
💡 Note: No segments overlap, so each segment appears as-is in the result with its original color.

Constraints

  • 1 ≤ segments.length ≤ 2 × 104
  • segments[i].length == 3
  • 1 ≤ starti < endi ≤ 105
  • 1 ≤ colori ≤ 109
  • All segments are half-closed intervals [start, end)

Visualization

Tap to expand
Paint Mixing Process VisualizationNumber Line (Canvas)Segment [1,4) Color: 5Segment [1,7) Color: 7After Color Mixing:Mixed [1,4): 5+7 = 12Pure [4,7): 7Sweep LineEvents:1: +5,+74: -57: -7Result: [[1,4,12],[4,7,7]]
Understanding the Visualization
1
Setup Canvas
Place all paint segments on the number line canvas
2
Mark Events
Identify critical points where painting starts or ends
3
Sweep & Mix
Move sweep line left to right, tracking active colors
4
Output Regions
Create final segments showing mixed color sums
Key Takeaway
🎯 Key Insight: Instead of checking every coordinate, we only process the 'events' where segments start or end. The sweep line technique efficiently tracks color mixing by maintaining the active color sum as we move through these critical points.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
38.4K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen