Class Solution

  • All Implemented Interfaces:

    
    public final class Solution
    
                        

    1589 - Maximum Sum Obtained of Any Permutation\.

    Medium

    We have an array of integers, nums, and an array of requests where <code>requestsi = start<sub>i</sub>, end<sub>i</sub></code>. The <code>i<sup>th</sup></code> request asks for the sum of <code>numsstart<sub>i</sub> + numsstart<sub>i</sub> + 1 + ... + numsend<sub>i</sub> - 1 + numsend<sub>i</sub></code>. Both <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> 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 <code>10<sup>9</sup> + 7</code>.

    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:

    requests0 -> nums1 + nums2 + nums3 = 1 + 3 + 4 = 8

    requests1 -> nums0 + nums1 = 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:

    requests0 -> nums1 + nums2 + nums3 = 5 + 4 + 2 = 11

    requests1 -> nums0 + nums1 = 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

    • <code>1 <= n <= 10<sup>5</sup></code>

    • <code>0 <= numsi<= 10<sup>5</sup></code>

    • <code>1 <= requests.length <= 10<sup>5</sup></code>

    • requests[i].length == 2

    • <code>0 <= start<sub>i</sub><= end<sub>i</sub>< n</code>

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
      Solution()
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      final Integer maxSumRangeQuery(IntArray nums, Array<IntArray> requests)
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait