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)/使数组中所有元素相等的最小操作数 II [minimum-operations-to-make-array-equal-ii].html
2023-01-23 20:16:24 +08:00

39 lines
1.9 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>nums1</code>&nbsp;<code>nums2</code>&nbsp;,两个数组长度都是&nbsp;<code>n</code>&nbsp;,再给你一个整数&nbsp;<code>k</code>&nbsp;。你可以对数组&nbsp;<code>nums1</code>&nbsp;进行以下操作:</p>
<ul>
<li>选择两个下标&nbsp;<code>i</code>&nbsp;<code>j</code>&nbsp;,将&nbsp;<code>nums1[i]</code>&nbsp;增加&nbsp;<code>k</code>&nbsp;,将&nbsp;<code>nums1[j]</code>&nbsp;减少&nbsp;<code>k</code>&nbsp;。换言之,<code>nums1[i] = nums1[i] + k</code>&nbsp;<code>nums1[j] = nums1[j] - k</code>&nbsp;</li>
</ul>
<p>如果对于所有满足&nbsp;<code>0 &lt;= i &lt; n</code>&nbsp;都有&nbsp;<code>num1[i] == nums2[i]</code>&nbsp;,那么我们称&nbsp;<code>nums1</code> <strong>等于</strong>&nbsp;<code>nums2</code>&nbsp;</p>
<p>请你返回使<em>&nbsp;</em><code>nums1</code><em> </em>等于<em>&nbsp;</em><code>nums2</code>&nbsp;&nbsp;<strong>最少</strong>&nbsp;操作数。如果没办法让它们相等,请你返回&nbsp;<code>-1</code>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>nums1 = [4,3,1,4], nums2 = [1,3,7,1], k = 3
<b>输出:</b>2
<b>解释:</b>我们可以通过 2 个操作将 nums1 变成 nums2 。
第 1 个操作i = 2 j = 0 。操作后得到 nums1 = [1,3,4,4] 。
第 2 个操作i = 2 j = 3 。操作后得到 nums1 = [1,3,7,1] 。
无法用更少操作使两个数组相等。</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums1 = [3,8,5,2], nums2 = [2,4,1,6], k = 1
<b>输出:</b>-1
<b>解释:</b>无法使两个数组相等。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums1.length == nums2.length</code></li>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= nums1[i], nums2[j] &lt;= 10<sup>9</sup></code></li>
<li><code>0 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>