Skip to content

Commit c441cb7

Browse files
committed
Add solution #370
1 parent d240061 commit c441cb7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@
358358
367|[Valid Perfect Square](./solutions/0367-valid-perfect-square.js)|Easy|
359359
368|[Largest Divisible Subset](./solutions/0368-largest-divisible-subset.js)|Medium|
360360
369|[Plus One Linked List](./solutions/0369-plus-one-linked-list.js)|Medium|
361+
370|[Range Addition](./solutions/0370-range-addition.js)|Medium|
361362
371|[Sum of Two Integers](./solutions/0371-sum-of-two-integers.js)|Medium|
362363
372|[Super Pow](./solutions/0372-super-pow.js)|Medium|
363364
373|[Find K Pairs with Smallest Sums](./solutions/0373-find-k-pairs-with-smallest-sums.js)|Medium|

solutions/0370-range-addition.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 370. Range Addition
3+
* https://leetcode.com/problems/range-addition/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer length and an array updates where
7+
* updates[i] = [startIdxi, endIdxi, inci].
8+
*
9+
* You have an array arr of length length with all zeros, and you have some operation
10+
* to apply on arr. In the ith operation, you should increment all the elements
11+
* arr[startIdxi], arr[startIdxi + 1], ..., arr[endIdxi] by inci.
12+
*
13+
* Return arr after applying all the updates.
14+
*/
15+
16+
/**
17+
* @param {number} length
18+
* @param {number[][]} updates
19+
* @return {number[]}
20+
*/
21+
var getModifiedArray = function(length, updates) {
22+
const result = new Array(length).fill(0);
23+
24+
for (const [start, end, inc] of updates) {
25+
result[start] += inc;
26+
if (end + 1 < length) result[end + 1] -= inc;
27+
}
28+
29+
for (let i = 1; i < length; i++) {
30+
result[i] += result[i - 1];
31+
}
32+
33+
return result;
34+
};

0 commit comments

Comments
 (0)