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 requests among all permutations of nums
.
Since the answer may be too large, return it modulo 109 + 7
.
Example 1:
Input: nums = [1,2,3,4,5], requests = [[1,3],[0,1]] Output: 19 Explanation: 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 = 8 requests[1] -> nums[0] + nums[1] = 2 + 1 = 3 Total 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 = 11 requests[1] -> nums[0] + nums[1] = 3 + 5 = 8 Total sum: 11 + 8 = 19, which is the best that you can do.
Example 2:
Input: nums = [1,2,3,4,5,6], requests = [[0,1]] Output: 11 Explanation: A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].
Example 3:
Input: nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]] Output: 47 Explanation: A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].
Constraints:
n == nums.length
1 <= n <= 105
0 <= nums[i] <= 105
1 <= requests.length <= 105
requests[i].length == 2
0 <= starti <= endi < n
class Solution:
def maxSumRangeQuery(self, nums: List[int], requests: List[List[int]]) -> int:
n = 100010
delta = [0] * n
for start, end in requests:
delta[start] += 1
delta[end + 1] -= 1
for i in range(1, n):
delta[i] += delta[i - 1]
nums.sort()
delta.sort()
i, j, res = n - 1, len(nums) - 1, 0
while i > 0 and delta[i] > 0:
res += delta[i] * nums[j]
i -= 1
j -= 1
return res % 1000000007
class Solution {
public int maxSumRangeQuery(int[] nums, int[][] requests) {
int n = 100010;
int[] delta = new int[n];
for (int[] request : requests) {
++delta[request[0]];
--delta[request[1] + 1];
}
for (int i = 1; i < n; ++i) {
delta[i] += delta[i - 1];
}
Arrays.sort(nums);
Arrays.sort(delta);
long res = 0;
for (int i = n - 1, j = nums.length - 1; i >= 0 && delta[i] > 0; --i, --j) {
res += (long) delta[i] * nums[j];
}
return (int) (res % 1000000007);
}
}
class Solution {
public:
int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {
int n = 100010;
vector<int> delta(n);
for (auto request : requests) {
++delta[request[0]];
--delta[request[1] + 1];
}
for (int i = 1; i < n; ++i) {
delta[i] += delta[i - 1];
}
sort(nums.begin(), nums.end());
sort(delta.begin(), delta.end());
long long res = 0;
for (int i = n - 1, j = nums.size() - 1; i >= 0 && delta[i] > 0; --i, --j) {
res += (long long)delta[i] * nums[j];
}
return (int)(res % 1000000007);
}
};
func maxSumRangeQuery(nums []int, requests [][]int) int {
n := 100010
delta := make([]int, n)
for _, request := range requests {
delta[request[0]]++
delta[request[1]+1]--
}
for i := 1; i < n; i++ {
delta[i] += delta[i-1]
}
sort.Ints(nums)
sort.Ints(delta)
i, j, res := n-1, len(nums)-1, 0
for i >= 0 && delta[i] > 0 {
res += delta[i] * nums[j]
i--
j--
}
return res % 1000000007
}