diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md index 982021b65e6e2..a2a961e7601c4 100644 --- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md +++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md @@ -31,7 +31,7 @@ tags:
输入:nums = [1,2,3,4], n = 4, left = 1, right = 5 -输出:13 +输出:13 解释:所有的子数组和为 1, 3, 6, 10, 2, 5, 9, 3, 7, 4 。将它们升序排序后,我们得到新的数组 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 1 到 ri = 5 的和为 1 + 2 + 3 + 3 + 4 = 13 。@@ -161,6 +161,54 @@ func rangeSum(nums []int, n int, left int, right int) (ans int) { } ``` +#### TypeScript + +```ts +function rangeSum(nums: number[], n: number, left: number, right: number): number { + let arr = Array((n * (n + 1)) / 2).fill(0); + const mod = 10 ** 9 + 7; + + for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) { + for (let j = i; j < n; j++, k++) { + s += nums[j]; + arr[k] = s; + } + } + + let ans = 0; + arr = arr.sort((a, b) => a - b).slice(left - 1, right); + for (const x of arr) { + ans += x; + } + + return ans % mod; +} +``` + +#### JavaScript + +```js +function rangeSum(nums, n, left, right) { + let arr = Array((n * (n + 1)) / 2).fill(0); + const mod = 10 ** 9 + 7; + + for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) { + for (let j = i; j < n; j++, k++) { + s += nums[j]; + arr[k] = s; + } + } + + let ans = 0; + arr = arr.sort((a, b) => a - b).slice(left - 1, right); + for (const x of arr) { + ans += x; + } + + return ans % mod; +} +``` + diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md index 9d2e4df2e3696..c746085a115ae 100644 --- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md +++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md @@ -30,8 +30,8 @@ tags:
Input: nums = [1,2,3,4], n = 4, left = 1, right = 5 -Output: 13 -Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13. +Output: 13 +Explanation: All subarray sums are 1, 3, 6, 10, 2, 5, 9, 3, 7, 4. After sorting them in non-decreasing order we have the new array [1, 2, 3, 3, 4, 5, 6, 7, 9, 10]. The sum of the numbers from index le = 1 to ri = 5 is 1 + 2 + 3 + 3 + 4 = 13.
Example 2:
@@ -65,7 +65,11 @@ tags: -### Solution 1 +### Solution 1: Sorting + +According to the problem statement, generate the `arr` array, sort it, and then sum all the elements in the range $[left-1,.. right-1]$ to get the result. + +Time complexity is $O(n^2 \times \log n)$, and space complexity is $O(n^2)$. Here, $n$ is the length of the array given in the problem. @@ -155,6 +159,54 @@ func rangeSum(nums []int, n int, left int, right int) (ans int) { } ``` +#### TypeScript + +```ts +function rangeSum(nums: number[], n: number, left: number, right: number): number { + let arr = Array((n * (n + 1)) / 2).fill(0); + const mod = 10 ** 9 + 7; + + for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) { + for (let j = i; j < n; j++, k++) { + s += nums[j]; + arr[k] = s; + } + } + + let ans = 0; + arr = arr.sort((a, b) => a - b).slice(left - 1, right); + for (const x of arr) { + ans += x; + } + + return ans % mod; +} +``` + +#### JavaScript + +```js +function rangeSum(nums, n, left, right) { + let arr = Array((n * (n + 1)) / 2).fill(0); + const mod = 10 ** 9 + 7; + + for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) { + for (let j = i; j < n; j++, k++) { + s += nums[j]; + arr[k] = s; + } + } + + let ans = 0; + arr = arr.sort((a, b) => a - b).slice(left - 1, right); + for (const x of arr) { + ans += x; + } + + return ans % mod; +} +``` + diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js new file mode 100644 index 0000000000000..83f07fdce38b7 --- /dev/null +++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js @@ -0,0 +1,19 @@ +function rangeSum(nums, n, left, right) { + let arr = Array((n * (n + 1)) / 2).fill(0); + const mod = 10 ** 9 + 7; + + for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) { + for (let j = i; j < n; j++, k++) { + s += nums[j]; + arr[k] = s; + } + } + + let ans = 0; + arr = arr.sort((a, b) => a - b).slice(left - 1, right); + for (const x of arr) { + ans += x; + } + + return ans % mod; +} diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts new file mode 100644 index 0000000000000..22e1796afe0a9 --- /dev/null +++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts @@ -0,0 +1,19 @@ +function rangeSum(nums: number[], n: number, left: number, right: number): number { + let arr = Array((n * (n + 1)) / 2).fill(0); + const mod = 10 ** 9 + 7; + + for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) { + for (let j = i; j < n; j++, k++) { + s += nums[j]; + arr[k] = s; + } + } + + let ans = 0; + arr = arr.sort((a, b) => a - b).slice(left - 1, right); + for (const x of arr) { + ans += x; + } + + return ans % mod; +}