Skip to content
Decoder

Range Addition

Medium

Problem

Start with an array of length zeros. Each update [start, end, inc] adds inc to every element from index start to index end, inclusive (indices are 0-based). Apply all updates and return the final array.

Example 1

length = 5, updates = [[1, 3, 2], [2, 4, 3], [0, 2, -2]]

Example 2

  →  [-2, 0, 3, 5, 3]

Example 3

Input
length = 3, updates = []
Output
[0, 0, 0]

Constraints

  • 1 <= length <= 10^5
  • 0 <= updates.length <= 10^4
  • 0 <= start <= end < length
  • -1000 <= inc <= 1000

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.