1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/子数组和排序后的区间和 [range-sum-of-sorted-subarray-sums].html
2022-03-29 12:43:11 +08:00

40 lines
1.7 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>n</code>&nbsp;个正整数。你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含&nbsp;<code>n * (n + 1) / 2</code>&nbsp;个数字的数组。</p>
<p>请你返回在新数组中下标为<em>&nbsp;</em><code>left</code>&nbsp;&nbsp;<code>right</code> <strong>(下标从 1 开始)</strong>的所有数字和(包括左右端点)。由于答案可能很大,请你将它对 10^9 + 7 取模后返回。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 5
<strong>输出:</strong>13
<strong>解释:</strong>所有的子数组和为 1, 3, 6, 10, 2, 5, 9, 3, 7, 4 。将它们升序排序后,我们得到新的数组 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 1 到 ri = 5 的和为 1 + 2 + 3 + 3 + 4 = 13 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 3, right = 4
<strong>输出:</strong>6
<strong>解释:</strong>给定数组与示例 1 一样,所以新数组为 [1, 2, 3, 3, 4, 5, 6, 7, 9, 10] 。下标从 le = 3 到 ri = 4 的和为 3 + 3 = 6 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,3,4], n = 4, left = 1, right = 10
<strong>输出:</strong>50
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= nums.length &lt;= 10^3</code></li>
<li><code>nums.length == n</code></li>
<li><code>1 &lt;= nums[i] &lt;= 100</code></li>
<li><code>1 &lt;= left &lt;= right&nbsp;&lt;= n * (n + 1) / 2</code></li>
</ul>