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 [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
Canvas Timeline[1,4)Day 1: +3 units[4,7)Day 2: +3 units[1,3)Day 3: +0 unitsTotal Result: [3, 3, 0]Red area painted on Day 1, Blue area on Day 2Day 3 attempt (dashed) overlaps with Day 1, so 0 new area
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).
Asked in
Google 85 Amazon 72 Microsoft 58 Meta 43
43.7K Views
Medium-High Frequency
~35 min Avg. Time
1.8K 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