forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.cpp
168 lines (137 loc) · 4.79 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/// Source : https://leetcode.com/problems/accounts-merge/description/
/// Author : liuyubobobo
/// Time : 2017-11-25
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
using namespace std;
/// Using Union-Find
/// Time Complexity: O(len(emails))
/// Space Complexity: O(len(emails))
class UnionFind{
private:
// rank[i]表示以i为根的集合所表示的树的层数
// 在后续的代码中, 我们并不会维护rank的语意, 也就是rank的值在路径压缩的过程中, 有可能不在是树的层数值
// 这也是我们的rank不叫height或者depth的原因, 他只是作为比较的一个标准
// 关于这个问题,可以参考问答区:http://coding.imooc.com/learn/questiondetail/7287.html
int* rank;
int* parent; // parent[i]表示第i个元素所指向的父节点
int count; // 数据个数
public:
// 构造函数
UnionFind(int count){
parent = new int[count];
rank = new int[count];
this->count = count;
for( int i = 0 ; i < count ; i ++ ){
parent[i] = i;
rank[i] = 1;
}
}
// 析构函数
~UnionFind(){
delete[] parent;
delete[] rank;
}
// 查找过程, 查找元素p所对应的集合编号
// O(h)复杂度, h为树的高度
int find(int p){
assert( p >= 0 && p < count );
// path compression 1
while( p != parent[p] ){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
// 查看元素p和元素q是否所属一个集合
// O(h)复杂度, h为树的高度
bool isConnected( int p , int q ){
return find(p) == find(q);
}
// 合并元素p和元素q所属的集合
// O(h)复杂度, h为树的高度
void unionElements(int p, int q){
int pRoot = find(p);
int qRoot = find(q);
if( pRoot == qRoot )
return;
// 根据两个元素所在树的元素个数不同判断合并方向
// 将元素个数少的集合合并到元素个数多的集合上
if( rank[pRoot] < rank[qRoot] ){
parent[pRoot] = qRoot;
}
else if( rank[qRoot] < rank[pRoot]){
parent[qRoot] = pRoot;
}
else{ // rank[pRoot] == rank[qRoot]
parent[pRoot] = qRoot;
rank[qRoot] += 1; // 此时, 我维护rank的值
}
}
};
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
map<string, string> emailToName;
for(const vector<string>& account: accounts)
for(int i = 1 ; i < account.size() ; i ++)
emailToName[account[i]] = account[0];
unordered_map<string, int> emailIndex;
int index = 0;
for(const pair<string, string>& p: emailToName)
emailIndex.insert(make_pair(p.first, index ++));
vector<string> intToEmail(emailIndex.size(), "");
for(const pair<string, int>& p: emailIndex)
intToEmail[p.second] = p.first;
UnionFind uf(emailIndex.size());
for(const vector<string>& account: accounts){
int index1 = emailIndex[account[1]];
for(int i = 2 ; i < account.size() ; i ++){
int index2 = emailIndex[account[i]];
uf.unionElements(index1, index2);
}
}
vector<vector<string>> res;
vector<bool> flag(emailIndex.size(), false);
for(int i = 0 ; i < emailIndex.size() ; i ++)
if(!flag[i]){
vector<string> emails;
emails.push_back(intToEmail[i]);
flag[i] = true;
for(int j = i + 1 ; j < emailIndex.size() ; j ++)
if(!flag[j] && uf.isConnected(i, j)){
flag[j] = true;
emails.push_back(intToEmail[j]);
}
sort(emails.begin(), emails.end());
string name = emailToName[emails[0]];
vector<string> tres;
tres.push_back(name);
for(const string& email: emails)
tres.push_back(email);
res.push_back(tres);
}
return res;
}
};
void printRes(const vector<vector<string>> res){
for(const vector<string>& row: res){
for(const string& e: row)
cout << e << " ";
cout << endl;
}
}
int main() {
vector<vector<string>> accounts = {
{"John", "[email protected]", "[email protected]"},
{"John", "[email protected]"},
{"John", "[email protected]", "[email protected]"},
{"Mary", "[email protected]"}
};
printRes(Solution().accountsMerge(accounts));
return 0;
}