1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/所有排列中的最大和 [maximum-sum-obtained-of-any-permutation].html
2022-03-29 12:43:11 +08:00

47 lines
2.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>有一个整数数组&nbsp;<code>nums</code>&nbsp;,和一个查询数组&nbsp;<code>requests</code>&nbsp;,其中&nbsp;<code>requests[i] = [start<sub>i</sub>, end<sub>i</sub>]</code>&nbsp;。第&nbsp;<code>i</code>&nbsp;个查询求&nbsp;<code>nums[start<sub>i</sub>] + nums[start<sub>i</sub> + 1] + ... + nums[end<sub>i</sub> - 1] + nums[end<sub>i</sub>]</code>&nbsp;的结果&nbsp;<code>start<sub>i</sub></code>&nbsp;<code>end<sub>i</sub></code>&nbsp;数组索引都是 <strong>从 0 开始</strong> 的。</p>
<p>你可以任意排列 <code>nums</code>&nbsp;中的数字,请你返回所有查询结果之和的最大值。</p>
<p>由于答案可能会很大,请你将它对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;后返回。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3,4,5], requests = [[1,3],[0,1]]
<strong>输出:</strong>19
<strong>解释:</strong>一个可行的 nums 排列为 [2,1,3,4,5],并有如下结果:
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 1 + 3 + 4 = 8
requests[1] -&gt; nums[0] + nums[1] = 2 + 1 = 3
总和为8 + 3 = 11。
一个总和更大的排列为 [3,5,4,2,1],并有如下结果:
requests[0] -&gt; nums[1] + nums[2] + nums[3] = 5 + 4 + 2 = 11
requests[1] -&gt; nums[0] + nums[1] = 3 + 5 = 8
总和为: 11 + 8 = 19这个方案是所有排列中查询之和最大的结果。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3,4,5,6], requests = [[0,1]]
<strong>输出:</strong>11
<strong>解释:</strong>一个总和最大的排列为 [6,5,4,3,2,1] ,查询和为 [11]。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>nums = [1,2,3,4,5,10], requests = [[0,2],[1,3],[1,1]]
<strong>输出:</strong>47
<strong>解释:</strong>一个和最大的排列为 [4,10,5,3,2,1] ,查询结果分别为 [19,18,10]。</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<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>