419. Range Addition
You are given an integer length and an array updates where updates[i] = [startIdxi, endIdxi, inci].
You have an array arr of length length with all zeros, and you have some operation to apply on arr. In the ith operation, you should increment all the elements arr[startIdxi], arr[startIdxi + 1], ..., arr[endIdxi] by inci.
Return arr after applying all the updates.
Example 1:
Input: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
Output: [-2, 0, 3, 5, 3]
Explanation:
- Apply [1,3,2] → arr = [0, 2, 2, 2, 0]
- Apply [2,4,3] → arr = [0, 2, 5, 5, 3]
- Apply [0,2,-2] → arr = [-2, 0, 3, 5, 3]
Example 2:
Input: length = 10, updates = [[2,4,6],[5,6,8],[1,9,-4]]
Output: [0, -4, 2, 2, 2, 4, 4, -4, -4, -4]
Explanation:
- Apply [2,4,6] → arr = [0, 0, 6, 6, 6, 0, 0, 0, 0, 0]
- Apply [5,6,8] → arr = [0, 0, 6, 6, 6, 8, 8, 0, 0, 0]
- Apply [1,9,-4] → arr = [0, -4, 2, 2, 2, 4, 4, -4, -4, -4]
Now Your Turn!
Pick the correct output for the given inputInput : lenght = 6 , updates = [[0,2,3],[1,4,-1],[3,5,2]]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= length <= 105
- 0 <= updates.length <= 104
- 0 <= startIdxi <= endIdxi < length
- -1000 <= inci <= 1000