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-number-of-operations-to-reinitialize-a-permutation].html
2022-03-29 12:43:11 +08:00

50 lines
1.5 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>n</code> ,已知存在一个长度为 <code>n</code> 的排列 <code>perm</code> ,其中 <code>perm[i] == i</code>​(下标 <strong>从 0 开始</strong> 计数)。</p>
<p>一步操作中,你将创建一个新数组 <code>arr</code> ,对于每个 <code>i</code> </p>
<ul>
<li>如果 <code>i % 2 == 0</code> ,那么 <code>arr[i] = perm[i / 2]</code></li>
<li>如果 <code>i % 2 == 1</code> ,那么 <code>arr[i] = perm[n / 2 + (i - 1) / 2]</code></li>
</ul>
<p>然后将 <code>arr</code> 赋值​​给 <code>perm</code></p>
<p>要想使 <code>perm</code> 回到排列初始值,至少需要执行多少步操作?返回最小的 <strong>非零</strong> 操作步数。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>n = 2
<strong>输出:</strong>1
<strong>解释:</strong>最初perm = [0,1]
第 1 步操作后perm = [0,1]
所以,仅需执行 1 步操作</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>n = 4
<strong>输出:</strong>2
<strong>解释:</strong>最初perm = [0,1,2,3]
第 1 步操作后perm = [0,2,1,3]
第 2 步操作后perm = [0,1,2,3]
所以,仅需执行 2 步操作</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>n = 6
<strong>输出:</strong>4
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>2 <= n <= 1000</code></li>
<li><code>n</code> 是一个偶数</li>
</ul>