Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f54cc81

Browse files
authored
feat: add js/ts solutions to lc problem: No.1508 (doocs#3361)
1 parent c61565e commit f54cc81

File tree

4 files changed

+142
-4
lines changed

4 files changed

+142
-4
lines changed

‎solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ tags:
3131

3232
<pre>
3333
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
34-
<strong>输出:</strong>13
34+
<strong>输出:</strong>13
3535
<strong>解释:</strong>所有的子数组和为 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 。
3636
</pre>
3737

@@ -161,6 +161,54 @@ func rangeSum(nums []int, n int, left int, right int) (ans int) {
161161
}
162162
```
163163

164+
#### TypeScript
165+
166+
```ts
167+
function rangeSum(nums: number[], n: number, left: number, right: number): number {
168+
let arr = Array((n * (n + 1)) / 2).fill(0);
169+
const mod = 10 ** 9 + 7;
170+
171+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
172+
for (let j = i; j < n; j++, k++) {
173+
s += nums[j];
174+
arr[k] = s;
175+
}
176+
}
177+
178+
let ans = 0;
179+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
180+
for (const x of arr) {
181+
ans += x;
182+
}
183+
184+
return ans % mod;
185+
}
186+
```
187+
188+
#### JavaScript
189+
190+
```js
191+
function rangeSum(nums, n, left, right) {
192+
let arr = Array((n * (n + 1)) / 2).fill(0);
193+
const mod = 10 ** 9 + 7;
194+
195+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
196+
for (let j = i; j < n; j++, k++) {
197+
s += nums[j];
198+
arr[k] = s;
199+
}
200+
}
201+
202+
let ans = 0;
203+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
204+
for (const x of arr) {
205+
ans += x;
206+
}
207+
208+
return ans % mod;
209+
}
210+
```
211+
164212
<!-- tabs:end -->
165213

166214
<!-- solution:end -->

‎solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/README_EN.md

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ tags:
3030

3131
<pre>
3232
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
33-
<strong>Output:</strong> 13
34-
<strong>Explanation:</strong> 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.
33+
<strong>Output:</strong> 13
34+
<strong>Explanation:</strong> 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.
3535
</pre>
3636

3737
<p><strong class="example">Example 2:</strong></p>
@@ -65,7 +65,11 @@ tags:
6565

6666
<!-- solution:start -->
6767

68-
### Solution 1
68+
### Solution 1: Sorting
69+
70+
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.
71+
72+
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.
6973

7074
<!-- tabs:start -->
7175

@@ -155,6 +159,54 @@ func rangeSum(nums []int, n int, left int, right int) (ans int) {
155159
}
156160
```
157161

162+
#### TypeScript
163+
164+
```ts
165+
function rangeSum(nums: number[], n: number, left: number, right: number): number {
166+
let arr = Array((n * (n + 1)) / 2).fill(0);
167+
const mod = 10 ** 9 + 7;
168+
169+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
170+
for (let j = i; j < n; j++, k++) {
171+
s += nums[j];
172+
arr[k] = s;
173+
}
174+
}
175+
176+
let ans = 0;
177+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
178+
for (const x of arr) {
179+
ans += x;
180+
}
181+
182+
return ans % mod;
183+
}
184+
```
185+
186+
#### JavaScript
187+
188+
```js
189+
function rangeSum(nums, n, left, right) {
190+
let arr = Array((n * (n + 1)) / 2).fill(0);
191+
const mod = 10 ** 9 + 7;
192+
193+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
194+
for (let j = i; j < n; j++, k++) {
195+
s += nums[j];
196+
arr[k] = s;
197+
}
198+
}
199+
200+
let ans = 0;
201+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
202+
for (const x of arr) {
203+
ans += x;
204+
}
205+
206+
return ans % mod;
207+
}
208+
```
209+
158210
<!-- tabs:end -->
159211

160212
<!-- solution:end -->
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function rangeSum(nums, n, left, right) {
2+
let arr = Array((n * (n + 1)) / 2).fill(0);
3+
const mod = 10 ** 9 + 7;
4+
5+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
6+
for (let j = i; j < n; j++, k++) {
7+
s += nums[j];
8+
arr[k] = s;
9+
}
10+
}
11+
12+
let ans = 0;
13+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
14+
for (const x of arr) {
15+
ans += x;
16+
}
17+
18+
return ans % mod;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function rangeSum(nums: number[], n: number, left: number, right: number): number {
2+
let arr = Array((n * (n + 1)) / 2).fill(0);
3+
const mod = 10 ** 9 + 7;
4+
5+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
6+
for (let j = i; j < n; j++, k++) {
7+
s += nums[j];
8+
arr[k] = s;
9+
}
10+
}
11+
12+
let ans = 0;
13+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
14+
for (const x of arr) {
15+
ans += x;
16+
}
17+
18+
return ans % mod;
19+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /