Sponsored
Sponsored
This approach uses a Union-Find (also known as Disjoint Set Union, DSU) data structure to count the number of swaps needed to pair the couples. By treating each couple as a connected component, we can iterate through the row and connect each person to their designated partner. The number of swaps needed is equivalent to the number of components minus one.
Time Complexity: O(N), where N is the number of seats, because each union-find operation is O(1) on average using path compression and union by rank.
Space Complexity: O(N) for the parent array to store the parent of each node.
1#include <vector>
2#include <iostream>
3using namespace std;
4
5class UnionFind {
6private:
7 vector<int> parent;
8public:
9 UnionFind(int n) {
10 parent.resize(n);
11 for (int i = 0; i < n; ++i) {
12 parent[i] = i;
13 }
14 }
15
16 int find(int x) {
17 if (parent[x] != x) {
18 parent[x] = find(parent[x]);
19 }
20 return parent[x];
21 }
22
23 void unite(int x, int y) {
24 int rootX = find(x), rootY = find(y);
25 if (rootX != rootY) {
26 parent[rootX] = rootY;
27 }
28 }
29};
30
31int minSwapsCouples(vector<int>& row) {
32 int n = row.size() / 2;
33 UnionFind uf(n);
34
35 for (int i = 0; i < row.size(); i += 2) {
36 int a = row[i], b = row[i + 1];
37 uf.unite(a / 2, b / 2);
38 }
39
40 int swaps = n;
41 for (int i = 0; i < n; ++i) {
42 if (uf.find(i) == i) swaps--;
43 }
44
45 return swaps;
46}
47
48int main() {
49 vector<int> row = {0, 2, 1, 3};
50 cout << "Minimum swaps required: " << minSwapsCouples(row) << endl;
51 return 0;
52}
This solution implements a Union-Find class to group couples efficiently. The row array is iterated over to unite the couple IDs by their respective roots. Finally, the code checks how many unique roots exist to determine the minimum swaps required.
This approach processes the row greedily to minimize swaps by placing each person in its correct position. Misplaced pairs turn into cycles; reducing each cycle requires one swap for each element in excess of two in the cycle. Thus, we can directly count cycles and deduce swaps.
Time Complexity: O(N), iterating the seats for correction.
Space Complexity: O(N), necessary to track positions of individuals.
1using System.Collections.Generic;
public class Solution {
public int MinSwapsCouples(int[] row) {
int n = row.Length;
int[] pos = new int[n];
for (int i = 0; i < n; i++) {
pos[row[i]] = i;
}
int swaps = 0;
for (int i = 0; i < n; i += 2) {
int partner = row[i] ^ 1;
if (row[i + 1] != partner) {
swaps++;
int swapWith = pos[partner];
pos[row[i + 1]] = swapWith;
int temp = row[i + 1];
row[i + 1] = row[swapWith];
row[swapWith] = temp;
}
}
return swaps;
}
public static void Main(string[] args) {
int[] row = {0, 2, 1, 3};
Solution sol = new Solution();
Console.WriteLine("Minimum swaps required: " + sol.MinSwapsCouples(row));
}
}
This C# solution iterates over the row, evaluating partner relationships through mathematical partners indices, and reorders using direct swaps, delivering a correct number of operations needed.