Skip to content

feat: add js/ts solutions to lc problem: No.1508 #3361

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ tags:

<pre>
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>输出:</strong>13
<strong>输出:</strong>13
<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 。
</pre>

Expand Down Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ tags:

<pre>
<strong>Input:</strong> nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>Output:</strong> 13
<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.
<strong>Output:</strong> 13
<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.
</pre>

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

<!-- solution:start -->

### 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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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;
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Loading