Skip to content

Commit 5c02ccf

Browse files
committed
feat: add solutions to lc problem: No.1508
No.1508.Range Sum of Sorted Subarray Sums
1 parent dcaecb5 commit 5c02ccf

File tree

6 files changed

+93
-50
lines changed

6 files changed

+93
-50
lines changed

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

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252

5353
**方法一:排序**
5454

55-
按照题意生成 `arr` 数组,排序后,对 `[left-1, right-1]` 范围的所有元素求和,得到结果。
55+
按照题意生成 `arr` 数组,排序后,对 $[left-1,.. right-1]$ 范围的所有元素求和,得到结果。
5656

57-
时间复杂度 $O(n^2\log n)$。
57+
时间复杂度 $O(n^2\log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为题目给定的数组长度
5858

5959
<!-- tabs:start -->
6060

@@ -72,8 +72,8 @@ class Solution:
7272
s += nums[j]
7373
arr.append(s)
7474
arr.sort()
75-
MOD = 10**9 + 7
76-
return sum(arr[left - 1 : right]) % MOD
75+
mod = 10**9 + 7
76+
return sum(arr[left - 1: right]) % mod
7777
```
7878

7979
### **Java**
@@ -82,32 +82,55 @@ class Solution:
8282

8383
```java
8484
class Solution {
85-
private static final int MOD = (int) 1e9 + 7;
86-
8785
public int rangeSum(int[] nums, int n, int left, int right) {
8886
int[] arr = new int[n * (n + 1) / 2];
89-
int idx = 0;
90-
for (int i = 0; i < n; ++i) {
87+
for (int i = 0, k = 0; i < n; ++i) {
9188
int s = 0;
9289
for (int j = i; j < n; ++j) {
9390
s += nums[j];
94-
arr[idx++] = s;
91+
arr[k++] = s;
9592
}
9693
}
9794
Arrays.sort(arr);
9895
int ans = 0;
96+
final int mod = (int) 1e9 + 7;
9997
for (int i = left - 1; i < right; ++i) {
100-
ans = (ans + arr[i]) % MOD;
98+
ans = (ans + arr[i]) % mod;
10199
}
102100
return ans;
103101
}
104102
}
105103
```
106104

105+
### **C++**
106+
107+
```cpp
108+
class Solution {
109+
public:
110+
int rangeSum(vector<int>& nums, int n, int left, int right) {
111+
int arr[n * (n + 1) / 2];
112+
for (int i = 0, k = 0; i < n; ++i) {
113+
int s = 0;
114+
for (int j = i; j < n; ++j) {
115+
s += nums[j];
116+
arr[k++] = s;
117+
}
118+
}
119+
sort(arr, arr + n * (n + 1) / 2);
120+
int ans = 0;
121+
const int mod = 1e9 + 7;
122+
for (int i = left - 1; i < right; ++i) {
123+
ans = (ans + arr[i]) % mod;
124+
}
125+
return ans;
126+
}
127+
};
128+
```
129+
107130
### **Go**
108131
109132
```go
110-
func rangeSum(nums []int, n int, left int, right int) int {
133+
func rangeSum(nums []int, n int, left int, right int) (ans int) {
111134
var arr []int
112135
for i := 0; i < n; i++ {
113136
s := 0
@@ -117,12 +140,11 @@ func rangeSum(nums []int, n int, left int, right int) int {
117140
}
118141
}
119142
sort.Ints(arr)
120-
mod := int(1e9) + 7
121-
ans := 0
122-
for i := left - 1; i < right; i++ {
123-
ans = (ans + arr[i]) % mod
143+
const mod int = 1e9 + 7
144+
for _, x := range arr[left-1 : right] {
145+
ans = (ans + x) % mod
124146
}
125-
return ans
147+
return
126148
}
127149
```
128150

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

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,40 +58,63 @@ class Solution:
5858
s += nums[j]
5959
arr.append(s)
6060
arr.sort()
61-
MOD = 10**9 + 7
62-
return sum(arr[left - 1 : right]) % MOD
61+
mod = 10**9 + 7
62+
return sum(arr[left - 1: right]) % mod
6363
```
6464

6565
### **Java**
6666

6767
```java
6868
class Solution {
69-
private static final int MOD = (int) 1e9 + 7;
70-
7169
public int rangeSum(int[] nums, int n, int left, int right) {
7270
int[] arr = new int[n * (n + 1) / 2];
73-
int idx = 0;
74-
for (int i = 0; i < n; ++i) {
71+
for (int i = 0, k = 0; i < n; ++i) {
7572
int s = 0;
7673
for (int j = i; j < n; ++j) {
7774
s += nums[j];
78-
arr[idx++] = s;
75+
arr[k++] = s;
7976
}
8077
}
8178
Arrays.sort(arr);
8279
int ans = 0;
80+
final int mod = (int) 1e9 + 7;
8381
for (int i = left - 1; i < right; ++i) {
84-
ans = (ans + arr[i]) % MOD;
82+
ans = (ans + arr[i]) % mod;
8583
}
8684
return ans;
8785
}
8886
}
8987
```
9088

89+
### **C++**
90+
91+
```cpp
92+
class Solution {
93+
public:
94+
int rangeSum(vector<int>& nums, int n, int left, int right) {
95+
int arr[n * (n + 1) / 2];
96+
for (int i = 0, k = 0; i < n; ++i) {
97+
int s = 0;
98+
for (int j = i; j < n; ++j) {
99+
s += nums[j];
100+
arr[k++] = s;
101+
}
102+
}
103+
sort(arr, arr + n * (n + 1) / 2);
104+
int ans = 0;
105+
const int mod = 1e9 + 7;
106+
for (int i = left - 1; i < right; ++i) {
107+
ans = (ans + arr[i]) % mod;
108+
}
109+
return ans;
110+
}
111+
};
112+
```
113+
91114
### **Go**
92115
93116
```go
94-
func rangeSum(nums []int, n int, left int, right int) int {
117+
func rangeSum(nums []int, n int, left int, right int) (ans int) {
95118
var arr []int
96119
for i := 0; i < n; i++ {
97120
s := 0
@@ -101,12 +124,11 @@ func rangeSum(nums []int, n int, left int, right int) int {
101124
}
102125
}
103126
sort.Ints(arr)
104-
mod := int(1e9) + 7
105-
ans := 0
106-
for i := left - 1; i < right; i++ {
107-
ans = (ans + arr[i]) % mod
127+
const mod int = 1e9 + 7
128+
for _, x := range arr[left-1 : right] {
129+
ans = (ans + x) % mod
108130
}
109-
return ans
131+
return
110132
}
111133
```
112134

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
class Solution {
22
public:
33
int rangeSum(vector<int>& nums, int n, int left, int right) {
4-
const int mod = 1e9 + 7;
5-
vector<int> arr;
6-
for (int i = 0; i < n; ++i) {
4+
int arr[n * (n + 1) / 2];
5+
for (int i = 0, k = 0; i < n; ++i) {
76
int s = 0;
87
for (int j = i; j < n; ++j) {
98
s += nums[j];
10-
arr.push_back(s);
9+
arr[k++] = s;
1110
}
1211
}
13-
sort(arr.begin(), arr.end());
12+
sort(arr, arr + n * (n + 1) / 2);
1413
int ans = 0;
15-
for (int i = left - 1; i < right; ++i) ans = (ans + arr[i]) % mod;
14+
const int mod = 1e9 + 7;
15+
for (int i = left - 1; i < right; ++i) {
16+
ans = (ans + arr[i]) % mod;
17+
}
1618
return ans;
1719
}
1820
};
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
func rangeSum(nums []int, n int, left int, right int) int {
1+
func rangeSum(nums []int, n int, left int, right int) (ans int) {
22
var arr []int
33
for i := 0; i < n; i++ {
44
s := 0
@@ -8,10 +8,9 @@ func rangeSum(nums []int, n int, left int, right int) int {
88
}
99
}
1010
sort.Ints(arr)
11-
mod := int(1e9) + 7
12-
ans := 0
13-
for i := left - 1; i < right; i++ {
14-
ans = (ans + arr[i]) % mod
11+
const mod int = 1e9 + 7
12+
for _, x := range arr[left-1 : right] {
13+
ans = (ans + x) % mod
1514
}
16-
return ans
15+
return
1716
}

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
class Solution {
2-
private static final int MOD = (int) 1e9 + 7;
3-
42
public int rangeSum(int[] nums, int n, int left, int right) {
53
int[] arr = new int[n * (n + 1) / 2];
6-
int idx = 0;
7-
for (int i = 0; i < n; ++i) {
4+
for (int i = 0, k = 0; i < n; ++i) {
85
int s = 0;
96
for (int j = i; j < n; ++j) {
107
s += nums[j];
11-
arr[idx++] = s;
8+
arr[k++] = s;
129
}
1310
}
1411
Arrays.sort(arr);
1512
int ans = 0;
13+
final int mod = (int) 1e9 + 7;
1614
for (int i = left - 1; i < right; ++i) {
17-
ans = (ans + arr[i]) % MOD;
15+
ans = (ans + arr[i]) % mod;
1816
}
1917
return ans;
2018
}

solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
77
s += nums[j]
88
arr.append(s)
99
arr.sort()
10-
MOD = 10**9 + 7
11-
return sum(arr[left - 1 : right]) % MOD
10+
mod = 10**9 + 7
11+
return sum(arr[left - 1: right]) % mod

0 commit comments

Comments
 (0)