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)/最小不兼容性 [minimum-incompatibility].html
2022-03-29 12:43:11 +08:00

46 lines
1.7 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>nums</code> 和一个整数 <code>k</code> 。你需要将这个数组划分到 <code>k</code> 个相同大小的子集中,使得同一个子集里面没有两个相同的元素。</p>
<p>一个子集的 <strong>不兼容性</strong> 是该子集里面最大值和最小值的差。</p>
<p>请你返回将数组分成 <code>k</code> 个子集后,各子集 <strong>不兼容性 </strong><strong></strong> 的 <strong>最小值</strong> ,如果无法分成分成 <code>k</code> 个子集,返回 <code>-1</code> 。</p>
<p>子集的定义是数组中一些数字的集合,对数字顺序没有要求。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<b>输入:</b>nums = [1,2,1,4], k = 2
<b>输出:</b>4
<b>解释:</b>最优的分配是 [1,2] 和 [1,4] 。
不兼容性和为 (2-1) + (4-1) = 4 。
注意到 [1,1] 和 [2,4] 可以得到更小的和,但是第一个集合有 2 个相同的元素,所以不可行。</pre>
<p><strong>示例 2</strong></p>
<pre>
<b>输入:</b>nums = [6,3,8,1,3,1,2,2], k = 4
<b>输出:</b>6
<b>解释:</b>最优的子集分配为 [1,2][2,3][6,8] 和 [1,3] 。
不兼容性和为 (2-1) + (3-2) + (8-6) + (3-1) = 6 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<b>输入:</b>nums = [5,3,3,6,3,3], k = 3
<b>输出:</b>-1
<b>解释:</b>没办法将这些数字分配到 3 个子集且满足每个子集里没有相同数字。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= k <= nums.length <= 16</code></li>
<li><code>nums.length</code> 能被 <code>k</code> 整除。</li>
<li><code>1 <= nums[i] <= nums.length</code></li>
</ul>