class Solution: def minSwapsCouples(self, row: List[int]) -> int: def find(x): if p[x] != x: p[x] = find(p[x]) return p[x] n = len(row) >> 1 p = list(range(n)) for i in range(0, len(row), 2): a, b = row[i] >> 1, row[i + 1] >> 1 p[find(a)] = find(b) return n - sum(i == find(i) for i in range(n))
We use cookies in order to offer you the most relevant information. See our Privacy Policy.