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)/分隔数组以得到最大和 [partition-array-for-maximum-sum].html
2022-03-29 12:43:11 +08:00

43 lines
1.4 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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>给你一个整数数组 <code>arr</code>,请你将该数组分隔为长度最多为 k 的一些(连续)子数组。分隔完成后,每个子数组的中的所有值都会变为该子数组中的最大值。</p>
<p>返回将数组分隔变换后能够得到的元素最大和。</p>
<p> </p>
<p><strong>注意,</strong>原数组和分隔后的数组对应顺序应当一致,也就是说,你只能选择分隔数组的位置而不能调整数组中的顺序。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>arr = [1,15,7,9,2,5,10], k = 3
<strong>输出:</strong>84
<strong>解释:</strong>
因为 k=3 可以分隔成 [1,15,7] [9] [2,5,10],结果为 [15,15,15,9,10,10,10],和为 84是该数组所有分隔变换后元素总和最大的。
若是分隔成 [1] [15,7,9] [2,5,10],结果就是 [1, 15, 15, 15, 10, 10, 10] 但这种分隔方式的元素总和76小于上一种。 </pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>arr = [1,4,1,5,7,3,6,1,9,9,3], k = 4
<strong>输出:</strong>83
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>arr = [1], k = 1
<strong>输出:</strong>1
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= arr.length <= 500</code></li>
<li><code>0 <= arr[i] <= 10<sup>9</sup></code></li>
<li><code>1 <= k <= arr.length</code></li>
</ul>