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)/执行交换操作后的最小汉明距离 [minimize-hamming-distance-after-swap-operations].html
2022-03-29 12:43:11 +08:00

45 lines
2.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>source</code><code>target</code> ,长度都是 <code>n</code> 。还有一个数组 <code>allowedSwaps</code> ,其中每个 <code>allowedSwaps[i] = [a<sub>i</sub>, b<sub>i</sub>]</code> 表示你可以交换数组 <code>source</code> 中下标为 <code>a<sub>i</sub></code><code>b<sub>i</sub></code><strong>下标从 0 开始</strong>)的两个元素。注意,你可以按 <strong>任意</strong> 顺序 <strong>多次</strong> 交换一对特定下标指向的元素。</p>
<p>相同长度的两个数组 <code>source</code><code>target</code> 间的 <strong>汉明距离</strong> 是元素不同的下标数量。形式上,其值等于满足 <code>source[i] != target[i]</code> <strong>下标从 0 开始</strong>)的下标 <code>i</code><code>0 &lt;= i &lt;= n-1</code>)的数量。</p>
<p>在对数组 <code>source</code> 执行 <strong>任意</strong> 数量的交换操作后,返回 <code>source</code><code>target</code> 间的 <strong>最小汉明距离</strong></p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><strong>输入:</strong>source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]
<strong>输出:</strong>1
<strong>解释:</strong>source 可以按下述方式转换:
- 交换下标 0 和 1 指向的元素source = [<strong>2</strong>,<strong>1</strong>,3,4]
- 交换下标 2 和 3 指向的元素source = [2,1,<strong>4</strong>,<strong>3</strong>]
source 和 target 间的汉明距离是 1 ,二者有 1 处元素不同,在下标 3 。
</pre>
<p><strong>示例 2</strong></p>
<pre><strong>输入:</strong>source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []
<strong>输出:</strong>2
<strong>解释:</strong>不能对 source 执行交换操作。
source 和 target 间的汉明距离是 2 ,二者有 2 处元素不同,在下标 1 和下标 2 。</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]
<strong>输出:</strong>0
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == source.length == target.length</code></li>
<li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>1 &lt;= source[i], target[i] &lt;= 10<sup>5</sup></code></li>
<li><code>0 &lt;= allowedSwaps.length &lt;= 10<sup>5</sup></code></li>
<li><code>allowedSwaps[i].length == 2</code></li>
<li><code>0 &lt;= a<sub>i</sub>, b<sub>i</sub> &lt;= n - 1</code></li>
<li><code>a<sub>i</sub> != b<sub>i</sub></code></li>
</ul>