Leetcode #1943: Describe the Painting
In this guide, we solve Leetcode #1943 Describe the Painting in Python and focus on the core idea that makes the solution efficient.
You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

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.
Quick Facts
- Difficulty: Medium
- Premium: No
- Tags: Array, Hash Table, Prefix Sum, Sorting
Intuition
Fast membership checks and value lookups are the heart of this problem, which makes a hash map the natural choice.
By storing what we have already seen (or counts/indexes), we can answer the question in one pass without backtracking.
Approach
Scan the input once, using the map to detect when the condition is satisfied and to update state as you go.
This keeps the solution linear while remaining easy to explain in an interview setting.
Steps:
- Initialize a hash map for seen items or counts.
- Iterate through the input, querying/updating the map.
- Return the first valid result or the final computed value.
Example
Input: segments = [[1,4,5],[4,7,7],[1,7,9]]
Output: [[1,4,14],[4,7,16]]
Explanation: The painting can be described as follows:
- [1,4) is colored {5,9} (with a sum of 14) from the first and third segments.
- [4,7) is colored {7,9} (with a sum of 16) from the second and third segments.
Python Solution
class Solution:
def splitPainting(self, segments: List[List[int]]) -> List[List[int]]:
d = defaultdict(int)
for l, r, c in segments:
d[l] += c
d[r] -= c
s = sorted([[k, v] for k, v in d.items()])
n = len(s)
for i in range(1, n):
s[i][1] += s[i - 1][1]
return [[s[i][0], s[i + 1][0], s[i][1]] for i in range(n - 1) if s[i][1]]
Complexity
The time complexity is O(n). The space complexity is O(n).
Edge Cases and Pitfalls
Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.
Summary
This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.