1722. Minimize Hamming Distance After Swap Operations

给定两个整数数组和允许交换元素索引的数组,可多次任意交换指定索引元素。汉明距离指相同长度数组元素不同的位置数量。要求通过对数组进行交换操作,求出两数组的最小汉明距离,解题思路是对允许交换的索引进行并查集操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given two integer arrays, source and target, both of length n. You are also given an array allowedSwaps where each allowedSwaps[i] = [ai, bi] indicates that you are allowed to swap the elements at index ai and index bi (0-indexed) of array source. Note that you can swap elements at a specific pair of indices multiple times and in any order.

The Hamming distance of two arrays of the same length, source and target, is the number of positions where the elements are different. Formally, it is the number of indices i for 0 <= i <= n-1 where source[i] != target[i] (0-indexed).

Return the minimum Hamming distance of source and target after performing any amount of swap operations on array source.

 

Example 1:

Input: source = [1,2,3,4], target = [2,1,4,5], allowedSwaps = [[0,1],[2,3]]
Output: 1
Explanation: source can be transformed the following way:
- Swap indices 0 and 1: source = [2,1,3,4]
- Swap indices 2 and 3: source = [2,1,4,3]
The Hamming distance of source and target is 1 as they differ in 1 position: index 3.

Example 2:

Input: source = [1,2,3,4], target = [1,3,2,4], allowedSwaps = []
Output: 2
Explanation: There are no allowed swaps.
The Hamming distance of source and target is 2 as they differ in 2 positions: index 1 and index 2.

Example 3:

Input: source = [5,1,2,4,3], target = [1,5,4,2,3], allowedSwaps = [[0,4],[4,2],[1,3],[1,4]]
Output: 0

 

Constraints:

  • n == source.length == target.length
  • 1 <= n <= 105
  • 1 <= source[i], target[i] <= 105
  • 0 <= allowedSwaps.length <= 105
  • allowedSwaps[i].length == 2
  • 0 <= ai, bi <= n - 1
  • ai != bi

Contest完整题解可见公众号,欢迎关注:https://mp.weixin.qq.com/s?__biz=MzIwMTk4MzkyOA==&tempkey=MTA5NV8wSGY1bXdvRTUzQnZOb0NYc1N4c0FHWUJhQlNiSEU1NWQ1Rm1PdmtTeXhiQkNOTURIbVpwX1NZMkd2YVFDSDY3UGJ0Sno2eU1oLXVMdVRQOHZMTjJHd043ckFYaFZiVmI3TFdOMkpld0JaUVpkeXJyV0FXLW5sX3cyYWc0eUdIbERtdnRvRkJoRjY2SDNWT0hkbndkcDZ5aW9IRGdJN0ZpRkRHaDhnfn4%3D&chksm=16e4d0d2219359c4352c2b28fec53b58767fe6983b39bae2928fa95bbacd425676f1185f35b0#rd

思路:对allowedSwaps进行union-find,属于同一个group的node可以任意交换

class Solution(object):
    def minimumHammingDistance(self, source, target, allowedSwaps):
        """
        :type source: List[int]
        :type target: List[int]
        :type allowedSwaps: List[List[int]]
        :rtype: int
        """
        n = len(source)
        fa = list(range(n))
        def find(a):
            while a!=fa[a]:
                a=fa[a]
            return a
        def union(a, b):
            aa,bb = find(a),find(b)
            fa[aa]=bb
        for a,b in allowedSwaps:
            union(a, b)

        from collections import defaultdict, Counter
        groups = defaultdict(list)
        for i,a in enumerate(fa):
            groups[find(a)].append(i)

        res = 0
        for group_indexes in groups.values():
            d_source = Counter([source[i] for i in group_indexes])
            d_target = Counter([target[i] for i in group_indexes])
            for k in d_target:
                res += max(d_target[k] - d_source[k], 0)
        return res

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值