Practice Describe the Painting
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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 the overlapping segments of the painting were mixed when it was painted. When two or more colors mix, they form a new color that can be represented as a set of mixed colors. For the sake of simplicity, you should only output the sum of the elements in the set rather than the full set. You want to describe the painting with the minimum number of non-overlapping half-closed segments of these mixed colors. These segments can be represented by the 2D array painting where painting[j] = [leftj, rightj, mixj] describes a half-closed segment [leftj, rightj) with the mixed color sum of mixj. Return the 2D array painting describing the finished painting (excluding 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.
Source: Describe the Painting on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
segments = [[1,4,5],[4,7,7],[1,7,9]]
[[1,4,14],[4,7,16]]
Split at the coordinates where a segment starts or ends: 1, 4 and 7. Between 1 and 4 the active colors are 5 and 9, giving 14. At 4, color 5 ends and color 7 begins, so the sum becomes 16. Because the intervals exclude their right endpoints, the change belongs to the interval starting at 4.
# Example 2
segments = [[1,7,9],[6,8,15],[8,10,7]]
[[1,6,9],[6,7,24],[7,8,15],[8,10,7]]
Sweep from left to right. Only color 9 is active from position 1 to 6. At 6, color 15 joins it and the sum becomes 24. At 7, color 9 ends, leaving 15. At 8, that color ends and color 7 starts. Record a segment for each resulting active-color set, excluding the unpainted regions before 1 and after 10.
# Example 3
segments = [[1,4,5],[1,4,7],[4,7,1],[4,7,11]]
[[1,4,12],[4,7,12]]
Before position 4, the active set is {5, 7}; afterwards it is {1, 11}. Both sums equal 12, but the sets differ, so the boundary must remain. Do not discard an event coordinate merely because its net sum change is zero. The output stores a sum as a compact label, not as proof that two mixtures are identical.
Constraints
How to solve Describe the Painting
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
The mixture can only change where a segment starts or ends, so record a signed color delta at every endpoint, sweep the sorted coordinates with a running sum, and emit one piece between consecutive coordinates whenever the sum is positive; every endpoint remains a boundary even when its net delta is zero.
Why brute force is not enough
Painting each segment unit by unit onto an array of 10^5 positions costs O(segments * length), about 2 * 10^9 for 2 * 10^4 segments, and still leaves you comparing color sets between neighbors to find the boundaries.
Approach
- For each segment, add its color to a map at the start coordinate and subtract it at the end coordinate; keep the keys even when deltas cancel, because a coordinate where one color stops and another begins is still a boundary.
- Sort the distinct coordinates, or use an array indexed by coordinate up to 10^5 together with a boundary flag per coordinate.
- Sweep left to right with a 64-bit running sum, applying the delta at each coordinate as you arrive.
- Before applying a delta, if the running sum is positive, emit the half-open piece from the previous coordinate to the current one with that sum; a zero sum means an unpainted gap and is skipped.
- Because every color is at least 1, a positive sum is exactly the condition that some segment covers the piece.
Walkthrough of Example 2
segments = [[1,7,9],[6,8,15],[8,10,7]] → [[1,6,9],[6,7,24],[7,8,15],[8,10,7]]
- Deltas: +9 at 1, -9 at 7, +15 at 6, -15 and +7 at 8 (net -8), -7 at 10. Sorted coordinates: 1, 6, 7, 8, 10.
- At 1 the sum becomes 9. Reaching 6, emit [1, 6, 9], then apply +15 for a sum of 24.
- Reaching 7, emit [6, 7, 24], then apply -9 for 15. Reaching 8, emit [7, 8, 15], then apply -8 for 7.
- Reaching 10, emit [8, 10, 7], then apply -7 for 0. Output [[1,6,9],[6,7,24],[7,8,15],[8,10,7]].
Time and space
Time O(n log n) · Space O(n). n is the number of segments; there are at most 2n distinct coordinates to sort and sweep, and the map stores one delta per coordinate.
Interview pitfalls
- Dropping a coordinate whose net delta is zero, which merges two different mixtures with equal sums; example 3 has 5 + 7 and 1 + 11 on either side of coordinate 4 and must stay split.
- Using 32-bit sums; up to 2 * 10^4 colors of 10^9 each can total 2 * 10^13.
- Emitting a piece while the running sum is zero, which outputs unpainted gaps.
- Applying the delta before emitting the piece that ends at the current coordinate, which shifts every mixture one piece to the left.
- Treating the end coordinate as inclusive; segments are half-open, so the piece ending at x does not include x.
Follow-up questions interviewers ask
- How would you output the actual color sets rather than their sums, and what does that do to the complexity?
- If segments can be added and removed online, which balanced structure supports updates and queries in logarithmic time?
- How does the approach change if adjacent pieces with identical color sets should be merged into one?
Further reading on the technique: Sweep line algorithm
Related LeetCode Problems
Practice more problems similar to Describe the Painting.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.