|
| 1 | +# [1589. Maximum Sum Obtained of Any Permutation (Medium)](https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/) |
| 2 | + |
| 3 | +<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] + ... + 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> |
| 4 | + |
| 5 | +<p>Return <em>the maximum total sum of all requests <strong>among all permutations</strong> of</em> <code>nums</code>.</p> |
| 6 | + |
| 7 | +<p>Since the answer may be too large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p> |
| 8 | + |
| 9 | +<p> </p> |
| 10 | +<p><strong>Example 1:</strong></p> |
| 11 | + |
| 12 | +<pre><strong>Input:</strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]] |
| 13 | +<strong>Output:</strong> 19 |
| 14 | +<strong>Explanation:</strong> One permutation of nums is [2,1,3,4,5] with the following result: |
| 15 | +requests[0] -> nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8 |
| 16 | +requests[1] -> nums[0] + nums[1] = 2 + 1 = 3 |
| 17 | +Total sum: 8 + 3 = 11. |
| 18 | +A permutation with a higher total sum is [3,5,4,2,1] with the following result: |
| 19 | +requests[0] -> nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11 |
| 20 | +requests[1] -> nums[0] + nums[1] = 3 + 5 = 8 |
| 21 | +Total sum: 11 + 8 = 19, which is the best that you can do. |
| 22 | +</pre> |
| 23 | + |
| 24 | +<p><strong>Example 2:</strong></p> |
| 25 | + |
| 26 | +<pre><strong>Input:</strong> nums = [1,2,3,4,5,6], requests = [[0,1]] |
| 27 | +<strong>Output:</strong> 11 |
| 28 | +<strong>Explanation:</strong> A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].</pre> |
| 29 | + |
| 30 | +<p><strong>Example 3:</strong></p> |
| 31 | + |
| 32 | +<pre><strong>Input:</strong> nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]] |
| 33 | +<strong>Output:</strong> 47 |
| 34 | +<strong>Explanation:</strong> A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>n == nums.length</code></li> |
| 41 | + <li><code>1 <= n <= 10<sup>5</sup></code></li> |
| 42 | + <li><code>0 <= nums[i] <= 10<sup>5</sup></code></li> |
| 43 | + <li><code>1 <= requests.length <= 10<sup>5</sup></code></li> |
| 44 | + <li><code>requests[i].length == 2</code></li> |
| 45 | + <li><code>0 <= start<sub>i</sub> <= end<sub>i</sub> < n</code></li> |
| 46 | +</ul> |
| 47 | + |
| 48 | + |
| 49 | +**Related Topics**: |
| 50 | +[Greedy](https://leetcode.com/tag/greedy/) |
| 51 | + |
| 52 | +## Solution 1. |
| 53 | + |
| 54 | +For each `requests[i]`, add `1` at `cnt[requests[i][0]]` and subtract `1` at `cnt[requests[i][1] + 1]`. Then compute prefix sum in-place on `cnt`, `cnt[i]` becomes the corresponding count of `nums[i]`. |
| 55 | + |
| 56 | +Sort `cnt` and `nums`, and the sum of `nums[i] * cnt[i]` is the answer. |
| 57 | + |
| 58 | +```cpp |
| 59 | +// OJ: https://leetcode.com/problems/maximum-sum-obtained-of-any-permutation/ |
| 60 | +// Author: github.com/lzl124631x |
| 61 | +// Time: O(NlogN) |
| 62 | +// Space: O(N) |
| 63 | +class Solution { |
| 64 | +public: |
| 65 | + int maxSumRangeQuery(vector<int>& A, vector<vector<int>>& R) { |
| 66 | + long mod = 1e9+7, N = A.size(), ans = 0; |
| 67 | + vector<int> cnt(N); |
| 68 | + for (int i = 0; i < R.size(); ++i) { |
| 69 | + if (R[i][1] < N - 1) cnt[R[i][1] + 1]--; |
| 70 | + cnt[R[i][0]]++; |
| 71 | + } |
| 72 | + for (int i = 1; i < N; ++i) cnt[i] += cnt[i - 1]; |
| 73 | + sort(begin(cnt), end(cnt)); |
| 74 | + sort(begin(A), end(A)); |
| 75 | + for (int i = 0; i < N; ++i) ans = (ans + (A[i] * cnt[i]) % mod) % mod; |
| 76 | + return ans; |
| 77 | + } |
| 78 | +}; |
| 79 | +``` |
0 commit comments