-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
AccountsMerge.cpp
174 lines (156 loc) · 6.55 KB
/
AccountsMerge.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
169
170
171
172
173
174
// Source : https://leetcode.com/problems/accounts-merge/
// Author : Hao Chen
// Date : 2019-03-29
/*****************************************************************************************************
*
* Given a list accounts, each element accounts[i] is a list of strings, where the first element
* accounts[i][0] is a name, and the rest of the elements are emails representing emails of the
* account.
*
* Now, we would like to merge these accounts. Two accounts definitely belong to the same person if
* there is some email that is common to both accounts. Note that even if two accounts have the same
* name, they may belong to different people as people could have the same name. A person can have
* any number of accounts initially, but all of their accounts definitely have the same name.
*
* After merging the accounts, return the accounts in the following format: the first element of each
* account is the name, and the rest of the elements are emails in sorted order. The accounts
* themselves can be returned in any order.
*
* Example 1:
*
* Input:
* "[email protected]"], ["Mary", "[email protected]"]]
*
* Explanation:
* The first and third John's are the same person as they have the common email "[email protected]".
* The second John and Mary are different people as none of their email addresses are used by other
* accounts.
*
* We could return these lists in any order, for example the answer [['Mary', '[email protected]'],
* ['John', '[email protected]'],
*
* Note:
* The length of accounts will be in the range [1, 1000].
* The length of accounts[i] will be in the range [1, 10].
* The length of accounts[i][j] will be in the range [1, 30].
******************************************************************************************************/
//Bad Performance Solution
class Solution_Time_Limit_Exceeded {
public:
// We can orginze all relevant emails to a chain,
// then we can use Union Find algorithm
// Besides, we also need to map the relationship between name and email.
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
unordered_map<string, string> emails_chains; // email chains
unordered_map<string, string> names; // names to email chains' head
//initialization
for(int i = 0 ; i<accounts.size();i++) {
auto& account = accounts[i];
auto& name = account[0];
for (int j=1; j<account.size(); j++) {
auto& email = account[j];
if ( names.find(email) == names.end() ) {
emails_chains[email] = email;
names[email] = name;
}
join(emails_chains, account[1], email);
}
}
//reform the emails
unordered_map<string, set<string>> res;
for( auto& acc : accounts ) {
string e = find(emails_chains, acc[1]);
res[e].insert(acc.begin()+1, acc.end());
}
//output the result
vector<vector<string>> result;
for (auto pair : res) {
vector<string> emails(pair.second.begin(), pair.second.end());
emails.insert(emails.begin(), names[pair.first]);
result.push_back(emails);
}
return result;
}
string find(unordered_map<string, string>& emails_chains,
string email) {
while( email != emails_chains[email] ){
email = emails_chains[email];
}
return email;
}
bool join(unordered_map<string, string>& emails_chains,
string& email1, string& email2) {
string e1 = find(emails_chains, email1);
string e2 = find(emails_chains, email2);
if ( e1 != e2 ) emails_chains[e1] = email2;
return e1 == e2;
}
};
//
// Performance Tunning
// -----------------
//
// The above algorithm need to do string comparison, it causes lots of efforts
// So, we allocated the ID for each email, and compare the ID would save the time.
//
// Furthermore, we can use the Group-Email-ID instead of email ID,
// this would save more time.
//
class Solution {
public:
// We can orginze all relevant emails to a chain,
// then we can use Union Find algorithm
// Besides, we also need to map the relationship between name and email.
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
unordered_map<string, int> emails_id; //using email ID for union find
unordered_map<int, int> emails_chains; // email chains
unordered_map<int, string> names; // email id & name
//initialization & join
for(int i = 0 ; i<accounts.size();i++) {
// using the account index as the emails group ID,
// this could simplify the emails chain.
emails_chains[i] = i;
auto& account = accounts[i];
auto& name = account[0];
for (int j=1; j<account.size(); j++) {
auto& email = account[j];
if ( emails_id.find(email) == emails_id.end() ) {
emails_id[email] = i;
names[i] = name;
}else {
join( emails_chains, i, emails_id[email] );
}
}
}
//reform the emails
unordered_map<int, set<string>> res;
for(int i=0; i<accounts.size(); i++) {
int idx = find(emails_chains, i);
res[idx].insert(accounts[i].begin()+1, accounts[i].end());
}
//output the result
vector<vector<string>> result;
for (auto pair : res) {
vector<string> emails( pair.second.begin(), pair.second.end() );
emails.insert(emails.begin(), names[pair.first]);
result.push_back(emails);
}
return result;
}
int find(unordered_map<int, int>& emails_chains, int id) {
while( id != emails_chains[id] ){
id = emails_chains[id];
}
return id;
}
bool join(unordered_map<int, int>& emails_chains, int id1, int id2) {
int e1 = find(emails_chains, id1);
int e2 = find(emails_chains, id2);
if ( e1 != e2 ) emails_chains[e1] = e2;
return e1 == e2;
}
};