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 03d4423

Browse files
committed
feat: add solutions to lc problem: No.1508
No.1508.Range Sum of Sorted Subarray Sums
1 parent ac50eef commit 03d4423

File tree

6 files changed

+181
-2
lines changed

6 files changed

+181
-2
lines changed

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

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,80 @@
5050

5151
<!-- 这里可写通用的实现逻辑 -->
5252

53+
**方法一:排序**
54+
55+
按照题意生成 arr 数组,排序后,对 `[left-1, right-1]` 范围的所有元素求和,得到结果。
56+
57+
时间复杂度 O(n2logn)。
58+
5359
<!-- tabs:start -->
5460

5561
### **Python3**
5662

5763
<!-- 这里可写当前语言的特殊实现逻辑 -->
5864

5965
```python
60-
66+
class Solution:
67+
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
68+
arr = []
69+
for i in range(n):
70+
s = 0
71+
for j in range(i, n):
72+
s += nums[j]
73+
arr.append(s)
74+
arr.sort()
75+
MOD = 10**9 + 7
76+
return sum(arr[left - 1: right]) % MOD
6177
```
6278

6379
### **Java**
6480

6581
<!-- 这里可写当前语言的特殊实现逻辑 -->
6682

6783
```java
84+
class Solution {
85+
private static final int MOD = (int) 1e9 + 7;
86+
87+
public int rangeSum(int[] nums, int n, int left, int right) {
88+
int[] arr = new int[n * (n + 1) / 2];
89+
int idx = 0;
90+
for (int i = 0; i < n; ++i) {
91+
int s = 0;
92+
for (int j = i; j < n; ++j) {
93+
s += nums[j];
94+
arr[idx++] = s;
95+
}
96+
}
97+
Arrays.sort(arr);
98+
int ans = 0;
99+
for (int i = left - 1; i < right; ++i) {
100+
ans = (ans + arr[i]) % MOD;
101+
}
102+
return ans;
103+
}
104+
}
105+
```
68106

107+
### **Go**
108+
109+
```go
110+
func rangeSum(nums []int, n int, left int, right int) int {
111+
var arr []int
112+
for i := 0; i < n; i++ {
113+
s := 0
114+
for j := i; j < n; j++ {
115+
s += nums[j]
116+
arr = append(arr, s)
117+
}
118+
}
119+
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
124+
}
125+
return ans
126+
}
69127
```
70128

71129
### **...**

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

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,65 @@
4949
### **Python3**
5050

5151
```python
52-
52+
class Solution:
53+
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
54+
arr = []
55+
for i in range(n):
56+
s = 0
57+
for j in range(i, n):
58+
s += nums[j]
59+
arr.append(s)
60+
arr.sort()
61+
MOD = 10**9 + 7
62+
return sum(arr[left - 1: right]) % MOD
5363
```
5464

5565
### **Java**
5666

5767
```java
68+
class Solution {
69+
private static final int MOD = (int) 1e9 + 7;
70+
71+
public int rangeSum(int[] nums, int n, int left, int right) {
72+
int[] arr = new int[n * (n + 1) / 2];
73+
int idx = 0;
74+
for (int i = 0; i < n; ++i) {
75+
int s = 0;
76+
for (int j = i; j < n; ++j) {
77+
s += nums[j];
78+
arr[idx++] = s;
79+
}
80+
}
81+
Arrays.sort(arr);
82+
int ans = 0;
83+
for (int i = left - 1; i < right; ++i) {
84+
ans = (ans + arr[i]) % MOD;
85+
}
86+
return ans;
87+
}
88+
}
89+
```
5890

91+
### **Go**
92+
93+
```go
94+
func rangeSum(nums []int, n int, left int, right int) int {
95+
var arr []int
96+
for i := 0; i < n; i++ {
97+
s := 0
98+
for j := i; j < n; j++ {
99+
s += nums[j]
100+
arr = append(arr, s)
101+
}
102+
}
103+
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
108+
}
109+
return ans
110+
}
59111
```
60112

61113
### **...**
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
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)
7+
{
8+
int s = 0;
9+
for (int j = i; j < n; ++j)
10+
{
11+
s += nums[j];
12+
arr.push_back(s);
13+
}
14+
}
15+
sort(arr.begin(), arr.end());
16+
int ans = 0;
17+
for (int i = left - 1; i < right; ++i) ans = (ans + arr[i]) % mod;
18+
return ans;
19+
}
20+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func rangeSum(nums []int, n int, left int, right int) int {
2+
var arr []int
3+
for i := 0; i < n; i++ {
4+
s := 0
5+
for j := i; j < n; j++ {
6+
s += nums[j]
7+
arr = append(arr, s)
8+
}
9+
}
10+
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
15+
}
16+
return ans
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
private static final int MOD = (int) 1e9 + 7;
3+
4+
public int rangeSum(int[] nums, int n, int left, int right) {
5+
int[] arr = new int[n * (n + 1) / 2];
6+
int idx = 0;
7+
for (int i = 0; i < n; ++i) {
8+
int s = 0;
9+
for (int j = i; j < n; ++j) {
10+
s += nums[j];
11+
arr[idx++] = s;
12+
}
13+
}
14+
Arrays.sort(arr);
15+
int ans = 0;
16+
for (int i = left - 1; i < right; ++i) {
17+
ans = (ans + arr[i]) % MOD;
18+
}
19+
return ans;
20+
}
21+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
3+
arr = []
4+
for i in range(n):
5+
s = 0
6+
for j in range(i, n):
7+
s += nums[j]
8+
arr.append(s)
9+
arr.sort()
10+
MOD = 10**9 + 7
11+
return sum(arr[left - 1: right]) % MOD

0 commit comments

Comments
(0)

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