Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- int ans = 0;
- vector<int> p;
- public:
- int find(int x){
- return (x == p[x]) ? x : (p[x] = find(p[x]));
- }
- void unionSet(int x, int y){
- int px = find(x);
- int py = find(y);
- if(px != py){
- p[py] = px;
- ans++;
- }
- }
- int minSwapsCouples(vector<int>& row) {
- p.resize(row.size());
- for(int i=0; i<row.size(); i+=2){
- p[row[i]] = row[i];
- p[row[i+1]] = row[i];
- }
- for(int i=0; i<row.size(); i+=2)
- unionSet(i,i+1);
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement