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 c583aaa

Browse files
committed
feat: add js solution to lc problem: No.1508
1 parent 000a349 commit c583aaa

File tree

3 files changed

+69
-2
lines changed

3 files changed

+69
-2
lines changed

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ tags:
6363

6464
<!-- description:end -->
6565

66-
## 解法
66+
## 解法: Brute Force
6767

6868
<!-- solution:start -->
6969

@@ -185,6 +185,30 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
185185
}
186186
```
187187

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+
188212
<!-- tabs:end -->
189213

190214
<!-- solution:end -->

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tags:
6565

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

68-
### Solution 1
68+
### Solution 1: Brute Force
6969

7070
<!-- tabs:start -->
7171

@@ -179,6 +179,30 @@ function rangeSum(nums: number[], n: number, left: number, right: number): numbe
179179
}
180180
```
181181

182+
#### JavaScript
183+
184+
```js
185+
function rangeSum(nums, n, left, right) {
186+
let arr = Array((n * (n + 1)) / 2).fill(0);
187+
const mod = 10 ** 9 + 7;
188+
189+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
190+
for (let j = i; j < n; j++, k++) {
191+
s += nums[j];
192+
arr[k] = s;
193+
}
194+
}
195+
196+
let ans = 0;
197+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
198+
for (const x of arr) {
199+
ans += x;
200+
}
201+
202+
return ans % mod;
203+
}
204+
```
205+
182206
<!-- tabs:end -->
183207

184208
<!-- 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+
}

0 commit comments

Comments
(0)

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