-
Notifications
You must be signed in to change notification settings - Fork 41
/
solution.js
36 lines (33 loc) · 848 Bytes
/
solution.js
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
35
36
/**
* @param {number[]} nums
* @param {number} n
* @param {number} left
* @param {number} right
* @return {number}
*/
var rangeSum = function (nums, n, left, right) {
const sums = [];
let sum = 0;
for (let i = 0; i < nums.length; i++) {
sum += nums[i];
sums.push(sum);
}
const count = nums.length - 1;
for (let i = 1; i < nums.length; i++) {
const count = nums.length - i;
const start = sums.length - count;
const end = sums.length;
const remove = nums[i - 1];
for (let j = start; j < end; j++) {
sums.push(sums[j] - remove);
}
}
sums.sort((a, b) => a - b);
left--;
let result = 0;
const mod = 10 ** 9 + 7;
for (let i = left; i < right; i++) {
result = (result + sums[i]) % mod;
}
return result;
};