Skip to content

Commit 000a349

Browse files
committed
feat: add ts solution to lc problem: No.1508
1 parent 653e7a6 commit 000a349

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
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
@@ -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,30 @@ 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+
164188
<!-- tabs:end -->
165189

166190
<!-- solution:end -->

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

Lines changed: 26 additions & 2 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>
@@ -155,6 +155,30 @@ func rangeSum(nums []int, n int, left int, right int) (ans int) {
155155
}
156156
```
157157

158+
#### TypeScript
159+
160+
```ts
161+
function rangeSum(nums: number[], n: number, left: number, right: number): number {
162+
let arr = Array((n * (n + 1)) / 2).fill(0);
163+
const mod = 10 ** 9 + 7;
164+
165+
for (let i = 0, s = 0, k = 0; i < n; i++, s = 0) {
166+
for (let j = i; j < n; j++, k++) {
167+
s += nums[j];
168+
arr[k] = s;
169+
}
170+
}
171+
172+
let ans = 0;
173+
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
174+
for (const x of arr) {
175+
ans += x;
176+
}
177+
178+
return ans % mod;
179+
}
180+
```
181+
158182
<!-- tabs:end -->
159183

160184
<!-- solution:end -->
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)