Skip to content

Commit e31efcf

Browse files
committedApr 17, 2025
Add solution #1508
1 parent 19c7bf9 commit e31efcf

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
 

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,336 LeetCode solutions in JavaScript
1+
# 1,337 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1152,6 +1152,7 @@
11521152
1504|[Count Submatrices With All Ones](./solutions/1504-count-submatrices-with-all-ones.js)|Medium|
11531153
1505|[Minimum Possible Integer After at Most K Adjacent Swaps On Digits](./solutions/1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js)|Hard|
11541154
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
1155+
1508|[Range Sum of Sorted Subarray Sums](./solutions/1508-range-sum-of-sorted-subarray-sums.js)|Medium|
11551156
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
11561157
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
11571158
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 1508. Range Sum of Sorted Subarray Sums
3+
* https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/
4+
* Difficulty: Medium
5+
*
6+
* You are given the array nums consisting of n positive integers. You computed the sum of all
7+
* non-empty continuous subarrays from the array and then sorted them in non-decreasing order,
8+
* creating a new array of n * (n + 1) / 2 numbers.
9+
*
10+
* Return the sum of the numbers from index left to index right (indexed from 1), inclusive,
11+
* in the new array. Since the answer can be a huge number return it modulo 109 + 7.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @param {number} n
17+
* @param {number} left
18+
* @param {number} right
19+
* @return {number}
20+
*/
21+
var rangeSum = function(nums, n, left, right) {
22+
const MOD = 1e9 + 7;
23+
const sums = [];
24+
25+
for (let start = 0; start < n; start++) {
26+
let currentSum = 0;
27+
for (let end = start; end < n; end++) {
28+
currentSum += nums[end];
29+
sums.push(currentSum);
30+
}
31+
}
32+
33+
sums.sort((a, b) => a - b);
34+
35+
let result = 0;
36+
for (let i = left - 1; i < right; i++) {
37+
result = (result + sums[i]) % MOD;
38+
}
39+
40+
return result;
41+
};

0 commit comments

Comments
 (0)
Please sign in to comment.