-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathaccounts-merge.py
365 lines (331 loc) · 13.7 KB
/
accounts-merge.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
"""
721. Accounts Merge
Medium
Given a list of accounts where 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 common email 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: accounts = [["John","[email protected]","[email protected]"],["John","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]]
Output: [["John","[email protected]","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]]
Explanation:
The first and second John's are the same person as they have the common email "[email protected]".
The third 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]'],
Example 2:
Input: accounts = [["Gabe","[email protected]","[email protected]","[email protected]"],["Kevin","[email protected]","[email protected]","[email protected]"],["Ethan","[email protected]","[email protected]","[email protected]"],["Hanzo","[email protected]","[email protected]","[email protected]"],["Fern","[email protected]","[email protected]","[email protected]"]]
Output: [["Ethan","[email protected]","[email protected]","[email protected]"],["Gabe","[email protected]","[email protected]","[email protected]"],["Hanzo","[email protected]","[email protected]","[email protected]"],["Kevin","[email protected]","[email protected]","[email protected]"],["Fern","[email protected]","[email protected]","[email protected]"]]
Constraints:
1 <= accounts.length <= 1000
2 <= accounts[i].length <= 10
1 <= accounts[i][j] <= 30
accounts[i][0] consists of English letters.
accounts[i][j] (for j > 0) is a valid email.
"""
# V0
# V1
# http://bookshadow.com/weblog/2017/11/05/leetcode-accounts-merge/
# https://blog.csdn.net/fuxuemingzhu/article/details/82913712
import collections
class Solution(object):
def accountsMerge(self, accounts):
"""
:type accounts: List[List[str]]
:rtype: List[List[str]]
"""
eset = set()
owner = {}
parent = {}
result = collections.defaultdict(list)
def findParent(e):
rank = 0
while parent[e] != e:
rank += 1
e = parent[e]
return e, rank
def merge(a, b):
pa, ra = findParent(a)
pb, rb = findParent(b)
if ra < rb: parent[pa] = pb
else: parent[pb] = pa
for account in accounts:
name, emails = account[0], account[1:]
for email in emails:
eset.add(email)
owner[email] = name
if email not in parent: parent[email] = email
for email in emails[1:]:
merge(emails[0], email)
for email in eset:
result[findParent(email)[0]].append(email)
return [[owner[k]] + sorted(v) for k, v in result.iteritems()]
# V1'
# https://www.jiuzhang.com/solution/accounts-merge/#tag-highlight-lang-python
class Solution:
"""
@param accounts: List[List[str]]
@return: return a List[List[str]]
"""
def accountsMerge(self, accounts):
self.initialize(len(accounts))
email_to_ids = self.get_email_to_ids(accounts)
# union
for email, ids in email_to_ids.items():
root_id = ids[0]
for id in ids[1:]:
self.union(id, root_id)
id_to_email_set = self.get_id_to_email_set(accounts)
merged_accounts = []
for user_id, email_set in id_to_email_set.items():
merged_accounts.append([
accounts[user_id][0],
*sorted(email_set),
])
return merged_accounts
def get_id_to_email_set(self, accounts):
id_to_email_set = {}
for user_id, account in enumerate(accounts):
root_user_id = self.find(user_id)
email_set = id_to_email_set.get(root_user_id, set())
for email in account[1:]:
email_set.add(email)
id_to_email_set[root_user_id] = email_set
return id_to_email_set
def get_email_to_ids(self, accounts):
email_to_ids = {}
for i, account in enumerate(accounts):
for email in account[1:]:
email_to_ids[email] = email_to_ids.get(email, [])
email_to_ids[email].append(i)
return email_to_ids
def initialize(self, n):
self.father = {}
for i in range(n):
self.father[i] = i
def union(self, id1, id2):
self.father[self.find(id1)] = self.find(id2)
def find(self, user_id):
path = []
while user_id != self.father[user_id]:
path.append(user_id)
user_id = self.father[user_id]
for u in path:
self.father[u] = user_id
return user_id
# V1
# IDEA : Depth First Search (DFS)
# https://leetcode.com/problems/accounts-merge/solution/
# JAVA
# class Solution {
# HashSet<String> visited = new HashSet<>();
# Map<String, List<String>> adjacent = new HashMap<String, List<String>>();
#
# private void DFS(List<String> mergedAccount, String email) {
# visited.add(email);
# // Add the email vector that contains the current component's emails
# mergedAccount.add(email);
#
# if (!adjacent.containsKey(email)) {
# return;
# }
#
# for (String neighbor : adjacent.get(email)) {
# if (!visited.contains(neighbor)) {
# DFS(mergedAccount, neighbor);
# }
# }
# }
#
# public List<List<String>> accountsMerge(List<List<String>> accountList) {
# int accountListSize = accountList.size();
#
# for (List<String> account : accountList) {
# int accountSize = account.size();
#
# // Building adjacency list
# // Adding edge between first email to all other emails in the account
# String accountFirstEmail = account.get(1);
# for (int j = 2; j < accountSize; j++) {
# String accountEmail = account.get(j);
#
# if (!adjacent.containsKey(accountFirstEmail)) {
# adjacent.put(accountFirstEmail, new ArrayList<String>());
# }
# adjacent.get(accountFirstEmail).add(accountEmail);
#
# if (!adjacent.containsKey(accountEmail)) {
# adjacent.put(accountEmail, new ArrayList<String>());
# }
# adjacent.get(accountEmail).add(accountFirstEmail);
# }
# }
#
# // Traverse over all th accounts to store components
# List<List<String>> mergedAccounts = new ArrayList<>();
# for (List<String> account : accountList) {
# String accountName = account.get(0);
# String accountFirstEmail = account.get(1);
#
# // If email is visited, then it's a part of different component
# // Hence perform DFS only if email is not visited yet
# if (!visited.contains(accountFirstEmail)) {
# List<String> mergedAccount = new ArrayList<>();
# // Adding account name at the 0th index
# mergedAccount.add(accountName);
#
# DFS(mergedAccount, accountFirstEmail);
# Collections.sort(mergedAccount.subList(1, mergedAccount.size()));
# mergedAccounts.add(mergedAccount);
# }
# }
#
# return mergedAccounts;
# }
# }
# V1
# IDEA : Disjoint Set Union (DSU)
# https://leetcode.com/problems/accounts-merge/solution/
# JAVA
# class DSU {
# int representative [];
# int size [];
#
# DSU(int sz) {
# representative = new int[sz];
# size = new int[sz];
#
# for (int i = 0; i < sz; ++i) {
# // Initially each group is its own representative
# representative[i] = i;
# // Intialize the size of all groups to 1
# size[i] = 1;
# }
# }
#
# // Finds the representative of group x
# public int findRepresentative(int x) {
# if (x == representative[x]) {
# return x;
# }
#
# // This is path compression
# return representative[x] = findRepresentative(representative[x]);
# }
#
# // Unite the group that contains "a" with the group that contains "b"
# public void unionBySize(int a, int b) {
# int representativeA = findRepresentative(a);
# int representativeB = findRepresentative(b);
#
# // If nodes a and b already belong to the same group, do nothing.
# if (representativeA == representativeB) {
# return;
# }
#
# // Union by size: point the representative of the smaller
# // group to the representative of the larger group.
# if (size[representativeA] >= size[representativeB]) {
# size[representativeA] += size[representativeB];
# representative[representativeB] = representativeA;
# } else {
# size[representativeB] += size[representativeA];
# representative[representativeA] = representativeB;
# }
# }
# }
#
# class Solution {
# public List<List<String>> accountsMerge(List<List<String>> accountList) {
# int accountListSize = accountList.size();
# DSU dsu = new DSU(accountListSize);
#
# // Maps email to their component index
# Map<String, Integer> emailGroup = new HashMap<>();
#
# for (int i = 0; i < accountListSize; i++) {
# int accountSize = accountList.get(i).size();
#
# for (int j = 1; j < accountSize; j++) {
# String email = accountList.get(i).get(j);
# String accountName = accountList.get(i).get(0);
#
# // If this is the first time seeing this email then
# // assign component group as the account index
# if (!emailGroup.containsKey(email)) {
# emailGroup.put(email, i);
# } else {
# // If we have seen this email before then union this
# // group with the previous group of the email
# dsu.unionBySize(i, emailGroup.get(email));
# }
# }
# }
#
# // Store emails corresponding to the component's representative
# Map<Integer, List<String>> components = new HashMap<Integer, List<String>>();
# for (String email : emailGroup.keySet()) {
# int group = emailGroup.get(email);
# int groupRep = dsu.findRepresentative(group);
#
# if (!components.containsKey(groupRep)) {
# components.put(groupRep, new ArrayList<String>());
# }
#
# components.get(groupRep).add(email);
# }
#
# // Sort the components and add the account name
# List<List<String>> mergedAccounts = new ArrayList<>();
# for (int group : components.keySet()) {
# List<String> component = components.get(group);
# Collections.sort(component);
# component.add(0, accountList.get(group).get(0));
# mergedAccounts.add(component);
# }
#
# return mergedAccounts;
# }
# }
# V2
# Time: O(nlogn), n is the number of total emails,
# and the max length ofemail is 320, p.s. {64}@{255}
# Space: O(n)
import collections
class UnionFind(object):
def __init__(self):
self.set = []
def get_id(self):
self.set.append(len(self.set))
return len(self.set)-1
def find_set(self, x):
if self.set[x] != x:
self.set[x] = self.find_set(self.set[x]) # path compression.
return self.set[x]
def union_set(self, x, y):
x_root, y_root = map(self.find_set, (x, y))
if x_root != y_root:
self.set[min(x_root, y_root)] = max(x_root, y_root)
class Solution(object):
def accountsMerge(self, accounts):
"""
:type accounts: List[List[str]]
:rtype: List[List[str]]
"""
union_find = UnionFind()
email_to_name = {}
email_to_id = {}
for account in accounts:
name = account[0]
for i in range(1, len(account)):
if account[i] not in email_to_id:
email_to_name[account[i]] = name
email_to_id[account[i]] = union_find.get_id()
union_find.union_set(email_to_id[account[1]],
email_to_id[account[i]])
result = collections.defaultdict(list)
for email in email_to_name.keys():
result[union_find.find_set(email_to_id[email])].append(email)
for emails in result.values():
emails.sort()
return [[email_to_name[emails[0]]] + emails
for emails in result.values()]