-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path1508.cpp
More file actions
24 lines (24 loc) · 687 Bytes
/
1508.cpp
File metadata and controls
24 lines (24 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int rangeSum(vector<int>& nums, int n, int left, int right) {
vector<int> sums;
vector<int> prefix(n + 1, 0);
for (int i = 0; i < n; ++i) {
prefix[i + 1] = prefix[i] + nums[i];
}
for (int i = 0; i < n; ++i) {
for (int j = i; j < n; ++j) {
int sum = prefix[j + 1] - prefix[i];
sums.push_back(sum);
}
}
sort(sums.begin(), sums.end());
long long res = 0;
long long mod = 1e9 + 7;
for (int i = left; i <= right; i++) {
res += sums[i - 1];
res %= mod;
}
return res;
}
};