Amount of New Area Painted Each Day - Problem
Imagine you're a painter working on a long, horizontal canvas that stretches across a number line. Each day, you receive instructions to paint a specific section of this canvas, represented by intervals
The challenge is that painting over already painted areas creates an uneven, messy result. You only want to paint each position on the canvas at most once.
Given a 2D array
Goal: Calculate the amount of fresh, unpainted area covered each day.
Input: Array of painting intervals for each day
Output: Array showing new area painted each day
[start, end].The challenge is that painting over already painted areas creates an uneven, messy result. You only want to paint each position on the canvas at most once.
Given a 2D array
paint where paint[i] = [start_i, end_i] represents the section you need to paint on day i, return an array showing how much new area you actually painted each day (excluding areas that were already painted on previous days).Goal: Calculate the amount of fresh, unpainted area covered each day.
Input: Array of painting intervals for each day
Output: Array showing new area painted each day
Input & Output
example_1.py โ Basic Example
$
Input:
paint = [[1,4],[4,7],[1,3]]
โบ
Output:
[3,3,0]
๐ก Note:
Day 1: Paint [1,4) - all new area, paint 3 units. Day 2: Paint [4,7) - all new area, paint 3 units. Day 3: Paint [1,3) - already painted on day 1, paint 0 new units.
example_2.py โ Overlapping Intervals
$
Input:
paint = [[1,5],[2,4]]
โบ
Output:
[4,0]
๐ก Note:
Day 1: Paint [1,5) - paint 4 units. Day 2: Paint [2,4) - this range is completely within [1,5) which was already painted, so 0 new units.
example_3.py โ Partial Overlaps
$
Input:
paint = [[1,4],[2,6],[3,8]]
โบ
Output:
[3,2,2]
๐ก Note:
Day 1: Paint [1,4) โ 3 units. Day 2: Paint [2,6) โ [2,4) already painted, only [4,6) is new โ 2 units. Day 3: Paint [3,8) โ [3,6) already painted, only [6,8) is new โ 2 units.
Constraints
- 1 โค paint.length โค 105
- paint[i].length == 2
- 0 โค starti < endi โค 5 ร 104
- Each interval represents [start, end) - start inclusive, end exclusive
Visualization
Tap to expand
Understanding the Visualization
1
Day 1: Paint [1,4)
Paint positions 1, 2, 3 on empty canvas โ 3 new units
2
Day 2: Paint [4,7)
Paint positions 4, 5, 6 on canvas โ 3 new units (no overlap)
3
Day 3: Paint [1,3)
Attempt to paint positions 1, 2 โ 0 new units (already painted)
Key Takeaway
๐ฏ Key Insight: Use coordinate compression to track only the segment boundaries rather than individual positions, reducing time complexity from O(sum of ranges) to O(n log n).
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code