Amount of New Area Painted Each Day - Problem

There is a long and thin painting that can be represented by a number line. You are given a 0-indexed 2D integer array paint of length n, where paint[i] = [starti, endi].

This means that on the i-th day you need to paint the area between starti and endi. Painting the same area multiple times will create an uneven painting so you only want to paint each area of the painting at most once.

Return an integer array worklog of length n, where worklog[i] is the amount of new area that you painted on the i-th day.

Input & Output

Example 1 — Basic Overlapping
$ Input: paint = [[1,4],[4,7],[5,8]]
Output: [3,3,1]
💡 Note: Day 1: paint [1,4) → 3 new units. Day 2: paint [4,7) → 3 new units. Day 3: paint [5,8) → only position 7 is new (positions 5,6 already painted) → 1 new unit.
Example 2 — Complete Overlap
$ Input: paint = [[1,4],[2,3],[4,6]]
Output: [3,0,2]
💡 Note: Day 1: paint [1,4) → 3 new units. Day 2: paint [2,3) → completely inside [1,4), 0 new units. Day 3: paint [4,6) → 2 new units.
Example 3 — No Overlap
$ Input: paint = [[1,2],[3,4],[5,6]]
Output: [1,1,1]
💡 Note: All intervals are disjoint, each day paints exactly 1 new unit.

Constraints

  • 1 ≤ paint.length ≤ 2 × 104
  • paint[i].length == 2
  • 0 ≤ starti < endi ≤ 5 × 104

Visualization

Tap to expand
Amount of New Area Painted Each Day INPUT Number Line (Painting Area) 0 1 2 3 4 5 6 7 8 Paint Intervals: Day 0: [1,4] Day 1: [4,7] Day 2: [5,8] Input Array: paint = [ [1,4], [4,7], [5,8] ] Each interval = area to paint that day ALGORITHM STEPS 1 Track Painted Areas Use set/array to mark painted positions 2 Process Each Day For each interval [s,e] check positions s to e-1 3 Count New Area Only count positions not yet painted 4 Update Worklog Store new area count for each day Execution Trace: Day 0: paint 1,2,3 = 3 new Day 1: paint 4,5,6 = 3 new Day 2: 5,6 done, 7 new = 1 Painted: {1,2,3,4,5,6,7} FINAL RESULT Final Painted Number Line: 0 1 2 3 4 5 6 7 8 Worklog Breakdown: Day 0: Painted [1,4] = 3 new units 3 Day 1: Painted [4,7] = 3 new units 3 Day 2: Painted [5,8], only 7 new = 1 1 Output: [3, 3, 1] OK - Total 7 units painted Key Insight: The Interval Merging approach tracks which positions are already painted using a set or marking array. For each day's interval, we only count positions that haven't been painted before, avoiding double-counting. This ensures each position is painted at most once, giving us the exact amount of NEW area per day. TutorialsPoint - Amount of New Area Painted Each Day | Interval Merging Approach
Asked in
Google 25 Amazon 18 Microsoft 12
34.5K Views
Medium Frequency
~35 min Avg. Time
892 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