We have an array of integers, nums, and an array of requests where
requests[i] = [starti, endi]. The ith request asks for the sum of nums[starti] + nums[starti + 1] + ... + nums[endi - 1] + nums[endi]. Both starti and endi are 0-indexed.
Return the maximum total sum of all requestsamong all permutations ofnums.
Since the answer may be too large, return it modulo10^9 + 7.
Input: nums =[1,2,3,4,5], requests =[[1,3],[0,1]]Output: 19Explanation: One permutation of nums is[2,1,3,4,5]with the following result:requests[0]-> nums[1]+ nums[2]+ nums[3]=1+3+4=8requests[1]-> nums[0]+ nums[1]=2+1=3Total sum:8+3=11.A permutation with a higher total sum is[3,5,4,2,1]with the following result:requests[0]-> nums[1]+ nums[2]+ nums[3]=5+4+2=11requests[1]-> nums[0]+ nums[1]=3+5=8Total sum:11+8=19, which is the best that you can do.
Input: nums =[1,2,3,4,5,10], requests =[[0,2],[1,3],[1,1]]Output: 47Explanation: A permutation with the max total sum is[4,10,5,3,2,1]with request sums [19,18,10].
To maximize the sum of all requests, we should assign the largest numbers in nums to the indices that are requested most often. By counting how many times each index is requested, sorting these counts, and matching the largest numbers to the most requested indices, we maximize the total sum.
classSolution {
public:int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {
int n = nums.size(), mod =1e9+7;
vector<int> freq(n+1);
for (auto& req : requests) {
freq[req[0]]++;
freq[req[1]+1]--;
}
for (int i =1; i < n; ++i) freq[i] += freq[i-1];
freq.pop_back();
sort(nums.rbegin(), nums.rend());
sort(freq.rbegin(), freq.rend());
long ans =0;
for (int i =0; i < n; ++i) ans = (ans +1L* nums[i] * freq[i]) % mod;
return ans;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
funcmaxSumRangeQuery(nums []int, requests [][]int) int {
n, mod:= len(nums), int(1e9+7)
freq:= make([]int, n+1)
for_, req:=rangerequests {
freq[req[0]]++freq[req[1]+1]-- }
fori:=1; i < n; i++ { freq[i] +=freq[i-1] }
freq = freq[:n]
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
sort.Sort(sort.Reverse(sort.IntSlice(freq)))
ans:=0fori:=0; i < n; i++ {
ans = (ans+nums[i]*freq[i]) %mod }
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution {
publicintmaxSumRangeQuery(int[] nums, int[][] requests) {
int n = nums.length, mod = (int)1e9+7;
int[] freq =newint[n+1];
for (int[] req : requests) {
freq[req[0]]++;
freq[req[1]+1]--;
}
for (int i = 1; i < n; ++i) freq[i]+= freq[i-1];
freq = Arrays.copyOf(freq, n);
Arrays.sort(nums);
Arrays.sort(freq);
long ans = 0;
for (int i = n-1; i >= 0; --i) ans = (ans + 1L * nums[i]* freq[i]) % mod;
return (int)ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
classSolution {
funmaxSumRangeQuery(nums: IntArray, requests: Array<IntArray>): Int {
val n = nums.size
val mod = 1_000_000_007
val freq = IntArray(n+1)
for (req in requests) {
freq[req[0]]++ freq[req[1]+1]-- }
for (i in1 until n) freq[i] += freq[i-1]
val freqArr = freq.sliceArray(0 until n)
nums.sortDescending()
freqArr.sortDescending()
var ans = 0Lfor (i in0 until n) ans = (ans + nums[i].toLong() * freqArr[i]) % mod
return ans.toInt()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
defmax_sum_range_query(nums: list[int], requests: list[list[int]]) -> int:
n, mod = len(nums), 10**9+7 freq = [0] * (n+1)
for s, e in requests:
freq[s] +=1 freq[e+1] -=1for i in range(1, n):
freq[i] += freq[i-1]
freq.pop()
nums.sort(reverse=True)
freq.sort(reverse=True)
ans = sum(x*y for x, y in zip(nums, freq)) % mod
return ans