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)/将数组分成两个数组并最小化数组和的差 [partition-array-into-two-arrays-to-minimize-sum-difference].html
2022-03-29 12:43:11 +08:00

44 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>给你一个长度为 <code>2 * n</code>&nbsp;的整数数组。你需要将&nbsp;<code>nums</code>&nbsp;分成&nbsp;<strong>两个</strong>&nbsp;长度为&nbsp;<code>n</code>&nbsp;的数组,分别求出两个数组的和,并 <strong>最小化</strong>&nbsp;两个数组和之&nbsp;<b>差的绝对值</b>&nbsp;<code>nums</code>&nbsp;中每个元素都需要放入两个数组之一。</p>
<p>请你返回&nbsp;<strong>最小</strong>&nbsp;的数组和之差。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><img alt="example-1" src="https://assets.leetcode.com/uploads/2021/10/02/ex1.png" style="width: 240px; height: 106px;"></p>
<pre><b>输入:</b>nums = [3,9,7,3]
<b>输出:</b>2
<strong>解释:</strong>最优分组方案是分成 [3,9] 和 [7,3] 。
数组和之差的绝对值为 abs((3 + 9) - (7 + 3)) = 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums = [-36,36]
<b>输出:</b>72
<strong>解释:</strong>最优分组方案是分成 [-36] 和 [36] 。
数组和之差的绝对值为 abs((-36) - (36)) = 72 。
</pre>
<p><strong>示例 3</strong></p>
<p><img alt="example-3" src="https://assets.leetcode.com/uploads/2021/10/02/ex3.png" style="width: 316px; height: 106px;"></p>
<pre><b>输入:</b>nums = [2,-1,0,4,-2,-9]
<b>输出:</b>0
<strong>解释:</strong>最优分组方案是分成 [2,4,-9] 和 [-1,0,-2] 。
数组和之差的绝对值为 abs((2 + 4 + -9) - (-1 + 0 + -2)) = 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 15</code></li>
<li><code>nums.length == 2 * n</code></li>
<li><code>-10<sup>7</sup> &lt;= nums[i] &lt;= 10<sup>7</sup></code></li>
</ul>