Approach
Sorting and greedy selection
For Amount of New Area Painted Each Day, the implementation first exposes a useful order, then scans that order while making locally justified choices.
- Choose the key that reveals the greedy or grouping structure.
- Sort the relevant records by that key.
- Scan in order, maintaining the invariant that makes each local choice safe.
Code notes
- 32 lines of Python from the credited upstream file 2158.py.
- The implementation visibly relies on sequence storage.
- No explicit loop blocks detected.
Complexity
Sorting is typically the dominant term unless the subsequent scan uses a more expensive nested operation.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1from sortedcontainers import SortedList2 3 4class Solution:5 def amountPainted(self, paint: list[list[int]]) -> list[int]:6 minDay = min(s for s, e in paint)7 maxDay = max(e for s, e in paint)8 ans = [0] * len(paint)9 10 runningIndices = SortedList()11 events = [] 12 13 for i, (start, end) in enumerate(paint):14 events.append((start, i, 1)) 15 events.append((end, i, -1)) 16 17 events.sort()18 19 i = 0 20 for day in range(minDay, maxDay):21 while i < len(events) and events[i][0] == day:22 day, index, type = events[i]23 if type == 1:24 runningIndices.add(index)25 else:26 runningIndices.remove(index)27 i += 128 if runningIndices:29 ans[runningIndices[0]] += 130 31 return ans32