题目描述: 有一个整数数组 nums ,和一个查询数组 requests ,其中 requests[i] = [starti, endi] 。第 i 个查询求 nums[
s
t
a
r
t
i
start_i
starti] + nums[
s
t
a
r
t
i
+
1
start_i + 1
starti+1] + ... + nums[
e
n
d
i
−
1
end_i - 1
endi−1] + nums[
e
n
d
i
end_i
endi] 的结果 ,
s
t
a
r
t
i
start_i
starti 和
e
n
d
i
end_i
endi数组索引都是 从 0 开始 的。 你可以任意排列 nums 中的数字,请你返回所有查询结果之和的最大值。 由于答案可能会很大,请你将它对
1
0
9
+
7
10^9+7
109+7 取余 后返回
参数要求:
n
=
=
n
u
m
s
.
l
e
n
g
t
h
n == nums.length
n==nums.length
1
<
=
n
<
=
1
0
5
1 <= n <= 10^5
1<=n<=105
0
<
=
n
u
m
s
[
i
]
<
=
1
0
5
0 <= nums[i] <= 10^5
0<=nums[i]<=105
1
<
=
r
e
q
u
e
s
t
s
.
l
e
n
g
t
h
<
=
1
0
5
1 <= requests.length <= 10^5
1<=requests.length<=105
r
e
q
u
e
s
t
s
[
i
]
.
l
e
n
g
t
h
=
=
2
requests[i].length == 2
requests[i].length==2
0
<
=
s
t
a
r
t
i
<
=
e
n
d
i
<
n
0 <= start_i <= end_i < n
0<=starti<=endi<n
publicintmaxSumRangeQuery(int[] nums,int[][] requests){int len = nums.length;Arrays.sort(nums);int[] count =newint[len];for(int i =0; i < requests.length; i++){int start = requests[i][0];int end = requests[i][1];
count[start]++;if(end<len-1){
count[end+1]--;}}for(int i =1; i < len; i++){
count[i]+= count[i-1];}Arrays.sort(count);long res =0;int mod =(int)1e9+7;int indexCount = len-1;int indexNum = len-1;while(indexCount>=0&&count[indexCount]!=0){
res =(res+(long)nums[indexNum]*count[indexCount])%mod;
indexCount--;
indexNum--;}return(int)res;}