forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
66 lines (52 loc) · 1.62 KB
/
main2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Source : https://leetcode.com/problems/couples-holding-hands/description/
/// Author : liuyubobobo
/// Time : 2018-06-17
#include <iostream>
#include <vector>
#include <cassert>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/// Graph Solution
/// Time Complexity: O(n)
/// Space Complexity: O(n)
class Solution {
public:
int minSwapsCouples(vector<int>& row) {
assert(row.size() % 2 == 0);
for(int i = 0 ; i < row.size() ; i ++)
row[i] = row[i] / 2;
// print_vec(row);
unordered_map<int, vector<int>> node_map;
for(int i = 0 ; i < row.size() ; i ++)
node_map[row[i]].push_back(i / 2);
vector<unordered_set<int>> g(row.size() / 2);
for(const pair<int, vector<int>>& p: node_map){
g[p.second[0]].insert(p.second[1]);
g[p.second[1]].insert(p.second[0]);
}
int component_num = 0;
vector<bool> visited(g.size(), false);
for(int i = 0 ; i < g.size() ; i ++)
if(!visited[i]){
component_num ++;
dfs(g, i, visited);
}
return g.size() - component_num;
}
private:
void dfs(const vector<unordered_set<int>>& g, int index, vector<bool>& visited){
visited[index] = true;
for(int node: g[index])
if(!visited[node])
dfs(g, node, visited);
return;
}
};
int main() {
vector<int> row1 = {0, 2, 1, 3};
cout << Solution().minSwapsCouples(row1) << endl;
vector<int> row2 = {3, 2, 0, 1};
cout << Solution().minSwapsCouples(row2) << endl;
return 0;
}