1695. Maximum Sum Obtained Of Any Permutation¶
Difficulty: Medium
LeetCode Problem View on GitHub
1695. Maximum Sum Obtained of Any Permutation
Medium
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.length1 <= n <= 1050 <= nums[i] <= 1051 <= requests.length <= 105requests[i].length == 20 <= starti <= endi < n
Solution¶
class Solution {
private final int mod = (int)(1e9 + 7);
private int freq[];
static class Pair {
int node, idx;
public Pair(int node, int idx) {
this.node = node;
this.idx = idx;
}
@Override
public String toString() {
return "(" + node + " " + idx + ")";
}
}
static class customSort implements Comparator<Pair> {
@Override
public int compare(Pair first, Pair second) {
return Integer.compare(second.node, first.node);
}
}
public int maxSumRangeQuery(int[] nums, int[][] requests) {
int n = nums.length;
freq = new int[n];
for (int query[] : requests) {
int l = query[0], r = query[1];
freq[l]++; if (r + 1 < n) freq[r + 1]--;
}
for (int i = 1; i < n; i++)
freq[i] += freq[i - 1];
ArrayList<Pair> maxFreq = new ArrayList<>();
for (int i = 0; i < n; i++) {
maxFreq.add(new Pair(freq[i], i));
}
Collections.sort(maxFreq, new customSort());
ArrayList<Integer> values = new ArrayList<>();
for (int ele : nums)
values.add(ele);
Collections.sort(values);
int idx = values.size() - 1;
int arr[] = new int[n];
for (int i = 0; i < n; i++)
arr[maxFreq.get(i).idx] = values.get(idx--);
int pref[] = new int[n];
pref[0] = arr[0];
for (int i = 1; i < n; i++)
pref[i] += pref[i - 1] + arr[i];
long res = 0;
for (int query[] : requests) {
int l = query[0], r = query[1];
long total = pref[r] * 1L;
if (l - 1 >= 0) {
total -= pref[l - 1] * 1L;
}
res = (res + total) % mod;
}
return (int)(res);
}
}
Complexity Analysis¶
- Time Complexity:
O(?) - Space Complexity:
O(?)
Approach¶
Detailed explanation of the approach will be added here