java.lang.Object
g1501_1600.s1589_maximum_sum_obtained_of_any_permutation.Solution

public class Solution extends Object
1589 - Maximum Sum Obtained of Any Permutation.<p>Medium</p> <p>We have an array of integers, <code>nums</code>, and an array of <code>requests</code> where <code>requests[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>. The <code>i<sup>th</sup></code> request asks for the sum of <code>nums[start<sub>i</sub>] + nums[start<sub>i</sub> + 1] + &hellip; + nums[end<sub>i</sub> - 1] + nums[end<sub>i</sub>]</code>. Both <code>start<sub>i</sub></code> and <code>end<sub>i</sub></code> are <em>0-indexed</em>.</p> <p>Return <em>the maximum total sum of all requests <strong>among all permutations</strong> of</em> <code>nums</code>.</p> <p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]]</p> <p><strong>Output:</strong> 19</p> <p><strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result:</p> <p>requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8</p> <p>requests[1] -> nums[0] + nums[1] = 2 + 1 = 3</p> <p>Total sum: 8 + 3 = 11.</p> <p>A permutation with a higher total sum is [3,5,4,2,1] with the following result:</p> <p>requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11</p> <p>requests[1] -> nums[0] + nums[1] = 3 + 5 = 8</p> <p>Total sum: 11 + 8 = 19, which is the best that you can do.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4,5,6], requests = [[0,1]]</p> <p><strong>Output:</strong> 11</p> <p><strong>Explanation:</strong> A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]</p> <p><strong>Output:</strong> 47</p> <p><strong>Explanation:</strong> A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].</p> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> <li><code>1 <= requests.length <= 10<sup>5</sup></code></li> <li><code>requests[i].length == 2</code></li> <li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < n</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • maxSumRangeQuery

      public int maxSumRangeQuery(int[] nums, int[][] requests)