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-array-changes-to-make-differences-equal].html
2024-08-06 08:46:50 +08:00

60 lines
2.3 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>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;<code>n</code>&nbsp;<strong>偶数</strong>&nbsp;,同时给你一个整数&nbsp;<code>k</code>&nbsp;</p>
<p>你可以对数组进行一些操作。每次操作中,你可以将数组中 <strong>任一</strong>&nbsp;元素替换为 <code>0</code>&nbsp;<code>k</code>&nbsp;之间的<strong>&nbsp;任一</strong>&nbsp;整数。</p>
<p>执行完所有操作以后,你需要确保最后得到的数组满足以下条件:</p>
<ul>
<li>存在一个整数 <code>X</code>&nbsp;,满足对于所有的&nbsp;<code>(0 &lt;= i &lt; n)</code>&nbsp;都有&nbsp;<code>abs(a[i] - a[n - i - 1]) = X</code>&nbsp;</li>
</ul>
<p>请你返回满足以上条件 <strong>最少</strong>&nbsp;修改次数。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [1,0,1,2,4,3], k = 4</span></p>
<p><span class="example-io"><b>输出:</b>2</span></p>
<p><strong>解释:</strong><br />
我们可以执行以下操作:</p>
<ul>
<li>&nbsp;<code>nums[1]</code>&nbsp;变为 2 ,结果数组为&nbsp;<code>nums = [1,<em><strong>2</strong></em>,1,2,4,3]</code>&nbsp;</li>
<li>&nbsp;<code>nums[3]</code>&nbsp;变为 3 ,结果数组为&nbsp;<code>nums = [1,2,1,<em><strong>3</strong></em>,4,3]</code>&nbsp;</li>
</ul>
<p>整数&nbsp;<code>X</code>&nbsp;为 2 。</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block">
<p><span class="example-io"><b>输入:</b>nums = [0,1,2,3,3,6,5,4], k = 6</span></p>
<p><span class="example-io"><b>输出:</b>2</span></p>
<p><strong>解释:</strong><br />
我们可以执行以下操作:</p>
<ul>
<li>&nbsp;<code>nums[3]</code>&nbsp;变为 0 ,结果数组为&nbsp;<code>nums = [0,1,2,<em><strong>0</strong></em>,3,6,5,4]</code>&nbsp;</li>
<li>&nbsp;<code>nums[4]</code>&nbsp;变为 4 ,结果数组为&nbsp;<code>nums = [0,1,2,0,<em><strong>4</strong></em>,6,5,4]</code>&nbsp;</li>
</ul>
<p>整数 <code>X</code>&nbsp;为 4 。</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 &lt;= n == nums.length &lt;= 10<sup>5</sup></code></li>
<li><code>n</code>&nbsp;是偶数。</li>
<li><code>0 &lt;= nums[i] &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>