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)/重排水果 [rearranging-fruits].html

39 lines
1.8 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>n</code> 个水果。给你两个下标从 <strong>0</strong> 开始的整数数组 <code>basket1</code><code>basket2</code> ,用以表示两个果篮中每个水果的交换成本。为了让两个果篮中水果的数量相等。为此,可以根据需要多次执行下述操作:</p>
<ul>
<li>选中两个下标 <code>i</code><code>j</code> ,并交换 <code>basket1</code> 中的第 <code>i</code> 个水果和 <code>basket2</code> 中的第 <code>j</code> 个水果。</li>
<li>交换的成本是 <code>min(basket1<sub>i</sub>,basket2<sub>j</sub>)</code></li>
</ul>
<p>根据果篮中水果的成本进行排序,如果排序后结果完全相同,则认为两个果篮相等。</p>
<p>返回使两个果篮相等的最小交换成本,如果无法使两个果篮相等,则返回<em> </em><code>-1</code><em> </em></p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>basket1 = [4,2,2,2], basket2 = [1,4,1,2]
<strong>输出:</strong>1
<strong>解释:</strong>交换 basket1 中下标为 1 的水果和 basket2 中下标为 0 的水果,交换的成本为 1 。此时basket1 = [4,1,2,2] 且 basket2 = [2,4,1,2] 。重排两个数组,发现二者相等。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>basket1 = [2,3,4,1], basket2 = [3,2,5,1]
<strong>输出:</strong>-1
<strong>解释:</strong>可以证明无法使两个果篮相等。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>basket1.length == bakste2.length</code></li>
<li><code>1 &lt;= basket1.length &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= basket1<sub>i</sub>,basket2<sub>i</sub> &lt;= 10<sup>9</sup></code></li>
</ul>