Skip to contentKEEP THE CRAFTTHE SANCTUARY

Apply Range Additions

Mark each range update at its two ends, then recover every value with one running sum.

The problem

Implement apply_range_additions(length, updates). Start with a list of length zeros. Each update is [left, right, delta] and adds delta to every position from left to right, both ends included. Return the list after all updates. There can be many long updates, so avoid touching every position of every range; aim for O(length + number of updates).

Starter code

def apply_range_additions(length, updates):
    pass

Public examples

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

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.