LeetCode-in-Java

1589. 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:

Solution

import java.util.Arrays;

public class Solution {
    public int maxSumRangeQuery(int[] nums, int[][] requests) {
        Arrays.sort(nums);
        int l = nums.length;
        int[] tempArr = new int[l];
        // requests[i][0] incrementing index element by 1 and for requests[i][1]+1 decrementing by 1
        // this will help me get the freq of occurance of each index of array 'nums' in
        // all 'requests' intervals when I compute the sum array of tempArr.
        for (int[] request : requests) {
            int a = request[0];
            int b = request[1] + 1;
            tempArr[a]++;
            if (b < l) {
                tempArr[b]--;
            }
        }
        int prev = 0;
        for (int i = 0; i < l; i++) {
            tempArr[i] += prev;
            prev = tempArr[i];
        }
        Arrays.sort(tempArr);
        int index = l - 1;
        long ans = 0;
        while (index >= 0) {
            if (tempArr[index] == 0) {
                break;
            }
            long x = tempArr[index] % 1000000007;
            long y = nums[index] % 1000000007;
            index--;
            ans += x * y;
        }
        return (int) (ans % 1000000007);
    }
}