题目描述
给你一个数组 nums
,它包含 n
个正整数。你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 n * (n + 1) / 2
个数字的数组。
请你返回在新数组中下标为 left
到 right
(下标从 1 开始)的所有数字和(包括左右端点)。由于答案可能很大,请你将它对 10^9 + 7 取模后返回。
示例 1:
输入:nums = [1,2,3,4], n = 4, left = 1, right = 5
输出:13
解释:所有的子数组和为 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 。
示例 2:
输入:nums = [1,2,3,4], n = 4, left = 3, right = 4
输出:6
解释:给定数组与示例 1 一样,所以新数组为 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 3 到 ri = 4 的和为 3 + 3 = 6 。
示例 3:
输入:nums = [1,2,3,4], n = 4, left = 1, right = 10
输出:50
提示:
1 <= nums.length <= 10^3
nums.length == n
1 <= nums[i] <= 100
1 <= left <= right <= n * (n + 1) / 2
解法
方法一:模拟
我们可以按照题目的要求,生成数组 $\textit{arr}$,然后对数组进行排序,最后求出 $[\textit{left}-1, \textit{right}-1]$ 范围的所有元素的和,得到结果。
时间复杂度 $O(n^2 \times \log n)$,空间复杂度 $O(n^2)$。其中 $n$ 为题目给定的数组长度。
| class Solution:
def rangeSum(self, nums: List[int], n: int, left: int, right: int) -> int:
arr = []
for i in range(n):
s = 0
for j in range(i, n):
s += nums[j]
arr.append(s)
arr.sort()
mod = 10**9 + 7
return sum(arr[left - 1 : right]) % mod
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 | class Solution {
public int rangeSum(int[] nums, int n, int left, int right) {
int[] arr = new int[n * (n + 1) / 2];
for (int i = 0, k = 0; i < n; ++i) {
int s = 0;
for (int j = i; j < n; ++j) {
s += nums[j];
arr[k++] = s;
}
}
Arrays.sort(arr);
int ans = 0;
final int mod = (int) 1e9 + 7;
for (int i = left - 1; i < right; ++i) {
ans = (ans + arr[i]) % mod;
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 | class Solution {
public:
int rangeSum(vector<int>& nums, int n, int left, int right) {
int arr[n * (n + 1) / 2];
for (int i = 0, k = 0; i < n; ++i) {
int s = 0;
for (int j = i; j < n; ++j) {
s += nums[j];
arr[k++] = s;
}
}
sort(arr, arr + n * (n + 1) / 2);
int ans = 0;
const int mod = 1e9 + 7;
for (int i = left - 1; i < right; ++i) {
ans = (ans + arr[i]) % mod;
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 | func rangeSum(nums []int, n int, left int, right int) (ans int) {
var arr []int
for i := 0; i < n; i++ {
s := 0
for j := i; j < n; j++ {
s += nums[j]
arr = append(arr, s)
}
}
sort.Ints(arr)
const mod int = 1e9 + 7
for _, x := range arr[left-1 : right] {
ans = (ans + x) % mod
}
return
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | 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;
}
}
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
return arr.reduce((acc, cur) => (acc + cur) % mod, 0);
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | 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;
}
}
arr = arr.sort((a, b) => a - b).slice(left - 1, right);
return arr.reduce((acc, cur) => acc + cur, 0) % mod;
}
|