Skip to content

Commit ae9321c

Browse files
committed
feat: add solutions to lc problem: No.0370
No.0370.Range Addition
1 parent 35eef48 commit ae9321c

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [字符串相乘](/solution/0000-0099/0043.Multiply%20Strings/README.md) - 高精度乘法
4444
- [区域和检索 - 数组不可变](/solution/0300-0399/0303.Range%20Sum%20Query%20-%20Immutable/README.md) - 前缀和
4545
- [二维区域和检索 - 矩阵不可变](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README.md) - 二维前缀和
46+
- [区间加法](/solution/0300-0399/0370.Range%20Addition/README.md) - 前缀和、差分
4647
- [ 用邮票贴满网格图](/solution/2100-2199/2132.Stamping%20the%20Grid/README.md) - 二维前缀和、二维差分
4748
<!-- 排序算法、待补充 -->
4849

README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
4242
- [Multiply Strings](/solution/0000-0099/0043.Multiply%20Strings/README_EN.md) - Multiply large numbers
4343
- [Range Sum Query - Immutable](/solution/0300-0399/0303.Range%20Sum%20Query%20-%20Immutable/README_EN.md) - Prefix sum
4444
- [Range Sum Query 2D - Immutable](/solution/0300-0399/0304.Range%20Sum%20Query%202D%20-%20Immutable/README_EN.md) - Prefix sum
45+
- [Range Addition](/solution/0300-0399/0370.Range%20Addition/README_EN.md) - Prefix sum, Difference array
4546
- [ Stamping the Grid](/solution/2100-2199/2132.Stamping%20the%20Grid/README_EN.md) - Prefix sum, Difference array
4647

4748
### 2. Search

solution/0300-0399/0370.Range Addition/README.md

+30-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@
3939

4040
**方法一:差分数组**
4141

42+
设 d 为差分数组。
43+
44+
给区间 [l, r] 中的每一个数加上 c,即 `d[l] += c, d[r + 1] -= c`
45+
46+
对 d 求前缀和,即可得到操作后的数组。
47+
4248
时间复杂度 O(n)。
4349

4450
**方法二:树状数组 + 差分思想**
@@ -68,9 +74,7 @@ class Solution:
6874
delta[start] += inc
6975
if end + 1 < length:
7076
delta[end + 1] -= inc
71-
for i in range(1, length):
72-
delta[i] += delta[i - 1]
73-
return delta
77+
return list(accumulate(delta))
7478
```
7579

7680
树状数组:
@@ -318,6 +322,29 @@ func getModifiedArray(length int, updates [][]int) []int {
318322
}
319323
```
320324

325+
### **JavaScript**
326+
327+
```js
328+
/**
329+
* @param {number} length
330+
* @param {number[][]} updates
331+
* @return {number[]}
332+
*/
333+
var getModifiedArray = function (length, updates) {
334+
let delta = new Array(length).fill(0);
335+
for (let [start, end, inc] of updates) {
336+
delta[start] += inc;
337+
if (end + 1 < length) {
338+
delta[end + 1] -= inc;
339+
}
340+
}
341+
for (let i = 1; i < length; ++i) {
342+
delta[i] += delta[i - 1];
343+
}
344+
return delta;
345+
};
346+
```
347+
321348
### **...**
322349

323350
```

solution/0300-0399/0370.Range Addition/README_EN.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ class Solution:
4949
delta[start] += inc
5050
if end + 1 < length:
5151
delta[end + 1] -= inc
52-
for i in range(1, length):
53-
delta[i] += delta[i - 1]
54-
return delta
52+
return list(accumulate(delta))
5553
```
5654

5755
```python
@@ -283,6 +281,29 @@ func getModifiedArray(length int, updates [][]int) []int {
283281
}
284282
```
285283

284+
### **JavaScript**
285+
286+
```js
287+
/**
288+
* @param {number} length
289+
* @param {number[][]} updates
290+
* @return {number[]}
291+
*/
292+
var getModifiedArray = function (length, updates) {
293+
let delta = new Array(length).fill(0);
294+
for (let [start, end, inc] of updates) {
295+
delta[start] += inc;
296+
if (end + 1 < length) {
297+
delta[end + 1] -= inc;
298+
}
299+
}
300+
for (let i = 1; i < length; ++i) {
301+
delta[i] += delta[i - 1];
302+
}
303+
return delta;
304+
};
305+
```
306+
286307
### **...**
287308

288309
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} length
3+
* @param {number[][]} updates
4+
* @return {number[]}
5+
*/
6+
var getModifiedArray = function (length, updates) {
7+
let delta = new Array(length).fill(0);
8+
for (let [start, end, inc] of updates) {
9+
delta[start] += inc;
10+
if (end + 1 < length) {
11+
delta[end + 1] -= inc;
12+
}
13+
}
14+
for (let i = 1; i < length; ++i) {
15+
delta[i] += delta[i - 1];
16+
}
17+
return delta;
18+
};

solution/0300-0399/0370.Range Addition/Solution.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ def getModifiedArray(self, length: int, updates: List[List[int]]) -> List[int]:
55
delta[start] += inc
66
if end + 1 < length:
77
delta[end + 1] -= inc
8-
for i in range(1, length):
9-
delta[i] += delta[i - 1]
10-
return delta
8+
return list(accumulate(delta))

0 commit comments

Comments
 (0)