From 000a3494716cddeaccd876b8021eea5df327decd Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 4 Aug 2024 19:50:01 +0300 Subject: [PATCH 1/4] feat: add ts solution to lc problem: No.1508 --- .../README.md | 26 ++++++++++++++++- .../README_EN.md | 28 +++++++++++++++++-- .../Solution.ts | 19 +++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.ts 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..d35008b84913d 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,30 @@ 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; +} +``` + 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..f1978f8e17939 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:

@@ -155,6 +155,30 @@ 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; +} +``` + 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; +} From c583aaa952689174067bb85392d966b35c8a2856 Mon Sep 17 00:00:00 2001 From: rain84 Date: Sun, 4 Aug 2024 19:51:21 +0300 Subject: [PATCH 2/4] feat: add js solution to lc problem: No.1508 --- .../README.md | 26 ++++++++++++++++++- .../README_EN.md | 26 ++++++++++++++++++- .../Solution.js | 19 ++++++++++++++ 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.js 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 d35008b84913d..bc5a552cf42a7 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 @@ -63,7 +63,7 @@ tags: -## 解法 +## 解法: Brute Force @@ -185,6 +185,30 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe } ``` +#### 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 f1978f8e17939..f84ca91a835ff 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 @@ -65,7 +65,7 @@ tags: -### Solution 1 +### Solution 1: Brute Force @@ -179,6 +179,30 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe } ``` +#### 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; +} From 986cd521addedf61beda93e5d3df4b4b67d19a40 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 5 Aug 2024 09:05:28 +0800 Subject: [PATCH 3/4] Update README_EN.md --- .../1508.Range Sum of Sorted Subarray Sums/README_EN.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 f84ca91a835ff..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 @@ -65,7 +65,11 @@ tags: -### Solution 1: Brute Force +### 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. From 176684bf346bd0a6beb508acd5c40a1732366b80 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Mon, 5 Aug 2024 09:05:47 +0800 Subject: [PATCH 4/4] Update README.md --- .../1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 bc5a552cf42a7..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 @@ -63,7 +63,7 @@ tags: -## 解法: Brute Force +## 解法