Skip to content

Commit ba221b3

Browse files
committed
✨ range sum of sorted subarray sums
1 parent 1de4f3a commit ba221b3

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

CATEGORY.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
- 9.链表
2626
148
2727

28+
- 10.回溯算法
29+
491
30+
2831
* 字符串
2932

3033
* 排序
@@ -40,8 +43,6 @@
4043

4144
* 二叉搜索树
4245

43-
- 回溯算法
44-
4546
- 位运算
4647

4748
* 并查集
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} n
4+
* @param {number} left
5+
* @param {number} right
6+
* @return {number}
7+
*/
8+
var rangeSum = function (nums, n, left, right) {
9+
const res = [];
10+
let index = 0;
11+
12+
for (let i = 0; i < nums.length; i++) {
13+
let sum = 0;
14+
for (let j = i; j < nums.length; j++) {
15+
sum += nums[j];
16+
res[index] = sum;
17+
index++;
18+
}
19+
}
20+
21+
res.sort((x, y) => x - y);
22+
23+
return (
24+
res.slice(left - 1, right).reduce((acc, x) => acc + x, 0) %
25+
(Math.pow(10, 9) + 7)
26+
);
27+
};

0 commit comments

Comments
 (0)