-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path0370-range-addition.js
More file actions
34 lines (30 loc) · 899 Bytes
/
0370-range-addition.js
File metadata and controls
34 lines (30 loc) · 899 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* 370. Range Addition
* https://leetcode.com/problems/range-addition/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {number} length
* @param {number[][]} updates
* @return {number[]}
*/
var getModifiedArray = function(length, updates) {
const result = new Array(length).fill(0);
for (const [start, end, inc] of updates) {
result[start] += inc;
if (end + 1 < length) result[end + 1] -= inc;
}
for (let i = 1; i < length; i++) {
result[i] += result[i - 1];
}
return result;
};