Skip to content

Commit 7ec6b21

Browse files
committed
1589
1 parent 6f0ead0 commit 7ec6b21

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,7 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
658658
861 | Score After Flipping Matrix | Medium | [Solution](leetcode/861.%20Score%20After%20Flipping%20Matrix)
659659
862 | Shortest Subarray with Sum at Least K | Hard | [Solution](leetcode/862.%20Shortest%20Subarray%20with%20Sum%20at%20Least%20K)
660660
863 | All Nodes Distance K in Binary Tree | Medium | [Solution](leetcode/863.%20All%20Nodes%20Distance%20K%20in%20Binary%20Tree)
661+
866 | Prime Palindrome | Medium | [Solution](leetcode/866.%20Prime%20Palindrome)
661662
867 | Transpose Matrix | Easy | [Solution](leetcode/867.%20Transpose%20Matrix)
662663
868 | Binary Gap | Easy | [Solution](leetcode/868.%20Binary%20Gap)
663664
869 | Reordered Power of 2 | Medium | [Solution](leetcode/869.%20Reordered%20Power%20of%202)
@@ -1060,7 +1061,9 @@ Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.googl
10601061
1583 | Count Unhappy Friends | Medium | [Solution](leetcode/1583.%20Count%20Unhappy%20Friends)
10611062
1584 | Min Cost to Connect All Points | Medium | [Solution](leetcode/1584.%20Min%20Cost%20to%20Connect%20All%20Points)
10621063
1585 | Check If String Is Transformable With Substring Sort Operations | Hard | [Solution](leetcode/1585.%20Check%20If%20String%20Is%20Transformable%20With%20Substring%20Sort%20Operations)
1064+
1589 | Maximum Sum Obtained of Any Permutation | Medium | [Solution](leetcode/1589.%20Maximum%20Sum%20Obtained%20of%20Any%20Permutation)
10631065
1591 | Strange Printer II | Hard | [Solution](leetcode/1591.%20Strange%20Printer%20II)
1066+
1595 | Minimum Cost to Connect Two Groups of Points | Hard | [Solution](leetcode/1595.%20Minimum%20Cost%20to%20Connect%20Two%20Groups%20of%20Points)
10641067

10651068
# License
10661069

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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>&nbsp;</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] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
16+
requests[1] -&gt; 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] -&gt; nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
20+
requests[1] -&gt; 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>&nbsp;</p>
37+
<p><strong>Constraints:</strong></p>
38+
39+
<ul>
40+
<li><code>n == nums.length</code></li>
41+
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
42+
<li><code>0 &lt;= nums[i]&nbsp;&lt;= 10<sup>5</sup></code></li>
43+
<li><code>1 &lt;= requests.length &lt;=&nbsp;10<sup>5</sup></code></li>
44+
<li><code>requests[i].length == 2</code></li>
45+
<li><code>0 &lt;= start<sub>i</sub>&nbsp;&lt;= end<sub>i</sub>&nbsp;&lt;&nbsp;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+
```

readme.sh

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ Please give this repo a :star: if it inspires you. Thanks. :blush:
2828
😩 Hate manually copy & pasting the example testcases when solving LeetCode problems?
2929
👉 Try my [LeetCode Testcase Extractor](https://liuzhenglai.com/post/5e6f2551e9a0d01760b274d8)
3030
31+
Now I'm using a Chrome Extension I developed -- [LeetCoder](https://chrome.google.com/webstore/detail/gkmoalkjclphfhgnhdlilebaolpdkgpf) -- to facilitate my having fun on LeetCode. Features including but not limited to:
32+
* showing stats
33+
* copying problem, answer and **testcases**
34+
* randomly picking a problem.
35+
3136
\# | Title | Difficulty | Solution
3237
---|---|---|---" >> $file
3338

0 commit comments

Comments
 (0)