Ctrl K

Batch of Segment Adjustments

Medium
You have an array of size cells, all starting at zero. You are given a list of changes changes, where changes[i] = [a, b, delta] means: add delta (possibly negative) to every cell with an index from a to b inclusive. Apply all the changes and return the resulting array.
Example 1
size=4, changes=[[0, 1, 5], [1, 3, -2]] → [5, 3, -2, -2]

The first change adds 5 to positions 0–1: [5, 5, 0, 0]. The second subtracts 2 from positions 1–3: [5, 3, -2, -2].

Example 2
size=3, changes=[] → [0, 0, 0]

No changes, so every cell stays at zero.

Constraints
  • 1 ≤ size ≤ 10⁵
  • 0 ≤ changes.length ≤ 10⁴
  • 0 ≤ a ≤ b < size
  • -1000 ≤ delta ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumProduct of All the Others MediumStudios for Recording Sessions MediumLongest Run with a Given Sum MediumA Stretch Divisible by k Medium
loading Python…
Input
size=4, changes=[[0, 1, 5], [1, 3, -2]]
Expected
[5, 3, -2, -2]

Run checks the first 3 tests in your browser. Submit checks all 105 tests on the server, hidden ones included.