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)/使数组互补的最少操作次数 [minimum-moves-to-make-array-complementary].html
2022-03-29 12:43:11 +08:00

48 lines
2.0 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>给你一个长度为<strong> 偶数</strong> <code>n</code> 的整数数组 <code>nums</code> 和一个整数 <code>limit</code> 。每一次操作,你可以将 <code>nums</code> 中的任何整数替换为 <code>1</code> 到 <code>limit</code> 之间的另一个整数。</p>
<p>如果对于所有下标 <code>i</code><strong>下标从 </strong><code>0</code><strong> 开始</strong><code>nums[i] + nums[n - 1 - i]</code> 都等于同一个数,则数组 <code>nums</code><strong>互补的</strong> 。例如,数组 <code>[1,2,3,4]</code> 是互补的,因为对于所有下标 <code>i</code> <code>nums[i] + nums[n - 1 - i] = 5</code></p>
<p>返回使数组 <strong>互补</strong><strong>最少</strong> 操作次数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,4,3], limit = 4
<strong>输出:</strong>1
<strong>解释:</strong>经过 1 次操作,你可以将数组 nums 变成 [1,2,<strong>2</strong>,3](加粗元素是变更的数字):
nums[0] + nums[3] = 1 + 3 = 4.
nums[1] + nums[2] = 2 + 2 = 4.
nums[2] + nums[1] = 2 + 2 = 4.
nums[3] + nums[0] = 3 + 1 = 4.
对于每个 i nums[i] + nums[n-1-i] = 4 ,所以 nums 是互补的。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,2,1], limit = 2
<strong>输出:</strong>2
<strong>解释:</strong>经过 2 次操作,你可以将数组 nums 变成 [<strong>2</strong>,2,2,<strong>2</strong>] 。你不能将任何数字变更为 3 ,因为 3 > limit 。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>nums = [1,2,1,2], limit = 2
<strong>输出:</strong>0
<strong>解释:</strong>nums 已经是互补的。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 <= n <= 10<sup>5</sup></code></li>
<li><code>1 <= nums[i] <= limit <= 10<sup>5</sup></code></li>
<li><code>n</code> 是偶数。</li>
</ul>