{"id":7388,"date":"2020-09-19T23:11:13","date_gmt":"2020-09-20T06:11:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7388"},"modified":"2020-09-19T23:13:01","modified_gmt":"2020-09-20T06:13:01","slug":"leetcode-1589-maximum-sum-obtained-of-any-permutation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1589-maximum-sum-obtained-of-any-permutation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1589. Maximum Sum Obtained of Any Permutation"},"content":{"rendered":"\n<p>We have an array of integers,&nbsp;<code>nums<\/code>, and an array of&nbsp;<code>requests<\/code>&nbsp;where&nbsp;<code>requests[i] = [start<sub>i<\/sub>, end<sub>i<\/sub>]<\/code>. The&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;request asks for the sum of&nbsp;<code>nums[start<sub>i<\/sub>] + nums[start<sub>i<\/sub>&nbsp;+ 1] + ... + nums[end<sub>i<\/sub>&nbsp;- 1] + nums[end<sub>i<\/sub>]<\/code>. Both&nbsp;<code>start<sub>i<\/sub><\/code>&nbsp;and&nbsp;<code>end<sub>i<\/sub><\/code>&nbsp;are&nbsp;<em>0-indexed<\/em>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the maximum total sum of all requests&nbsp;<strong>among all permutations<\/strong>&nbsp;of<\/em>&nbsp;<code>nums<\/code>.<\/p>\n\n\n\n<p>Since the answer may be too large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5], requests = [[1,3],[0,1]]\n<strong>Output:<\/strong> 19\n<strong>Explanation:<\/strong> One permutation of nums is [2,1,3,4,5] with the following result: \nrequests[0] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8\nrequests[1] -&gt; nums[0] + nums[1] = 2 + 1 = 3\nTotal sum: 8 + 3 = 11.\nA permutation with a higher total sum is [3,5,4,2,1] with the following result:\nrequests[0] -&gt; nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11\nrequests[1] -&gt; nums[0] + nums[1] = 3 + 5  = 8\nTotal sum: 11 + 8 = 19, which is the best that you can do.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5,6], requests = [[0,1]]\n<strong>Output:<\/strong> 11\n<strong>Explanation:<\/strong> A permutation with the max total sum is [6,5,4,3,2,1] with request sums [11].<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]\n<strong>Output:<\/strong> 47\n<strong>Explanation:<\/strong> A permutation with the max total sum is [4,10,5,3,2,1] with request sums [19,18,10].<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == nums.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= nums[i]&nbsp;&lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= requests.length &lt;=&nbsp;10<sup>5<\/sup><\/code><\/li><li><code>requests[i].length == 2<\/code><\/li><li><code>0 &lt;= start<sub>i<\/sub>&nbsp;&lt;= end<sub>i<\/sub>&nbsp;&lt;&nbsp;n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy + Sweep line<\/strong><\/h2>\n\n\n\n<p>Sort the numbers, and sort the frequency of each index, it&#8217;s easy to show largest number with largest frequency gives us max sum.<\/p>\n\n\n\n<p>ans = sum(nums[i] * freq[i])<\/p>\n\n\n\n<p>We can use sweep line to compute the frequency of each index in O(n) time and space.<\/p>\n\n\n\n<p>For each request [start, end] : ++freq[start], &#8211;freq[end + 1]<\/p>\n\n\n\n<p>Then the prefix sum of freq array is the frequency for each index.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maxSumRangeQuery(vector<int>& nums, vector<vector<int>>& requests) {\n    constexpr int kMod = 1e9 + 7;\n    const int n = nums.size();    \n    vector<long> freq(n);\n    for (const auto& r : requests) {\n      ++freq[r[0]];\n      if (r[1] + 1 < n) --freq[r[1] + 1];\n    }\n    for (int i = 1; i < n; ++i)\n      freq[i] += freq[i - 1];\n    \n    sort(begin(freq), end(freq));\n    sort(begin(nums), end(nums));\n    \n    long ans = 0;\n    for (int i = 0; i < n; ++i)\n      ans += freq[i] * nums[i];\n    \n    return ans % kMod;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We have an array of integers,&nbsp;nums, and an array of&nbsp;requests&nbsp;where&nbsp;requests[i] = [starti, endi]. The&nbsp;ith&nbsp;request asks for the sum of&nbsp;nums[starti] + nums[starti&nbsp;+ 1] + &#8230; +&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,104],"class_list":["post-7388","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-sweep-line","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7388","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7388"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7388\/revisions"}],"predecessor-version":[{"id":7390,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7388\/revisions\/7390"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}