Imagine you're a game developer working on a score multiplier system. You start with an array of zeros representing base scores, and you need to apply multiple range updates to boost scores in specific segments.
Given an integer length and an array updates where updates[i] = [startIdx, endIdx, inc], you have an array arr of length length with all zeros initially.
For each update operation, you need to increment all elements from arr[startIdx] to arr[endIdx] (inclusive) by inc.
Goal: Return the final array after applying all updates efficiently.
Example: If length = 5 and updates = [[1,3,2],[2,4,3],[0,2,-2]], the final array should be [-2, 0, 3, 5, 3].
Input & Output
Visualization
Time & Space Complexity
Where k is the number of updates and n is the array length. We process each update in O(1) and then do one O(n) prefix sum pass
We need the difference array of size n (or n+1 to handle end+1 safely)
Constraints
- 1 โค length โค 105
- 0 โค updates.length โค 104
- 0 โค startIdx โค endIdx < length
- -1000 โค inc โค 1000