Apply many range updates with differences
A difference table records where a change starts and where it ends. Instead of touching every cell in every range, add a delta at the left boundary and undo it at the right boundary. One final running sum reconstructs every cell. This is useful when all updates arrive before any answers are needed.
difference = [0, 4, 0, -4]
current = 0
for delta in difference:
current += delta
print(current)Think before coding
The negative boundary cancels a range exactly where it stops. Overlaps add naturally in the difference table. The total work is proportional to the number of updates plus the output size. Work through the example by hand before changing the starter. Decide what should happen for an empty input, a single value and a repeated value where those cases apply. The checks use several inputs so a hard-coded answer cannot stand in for the rule.
Your task
Write apply_updates(n, updates). Begin with n zeros. For each (left, right, delta), add delta to indices in [left, right). Bounds are valid. Return the final list, including overlapping and negative updates.
The explanation opens here once every check passes.