-
Notifications
You must be signed in to change notification settings - Fork 17
/
accounts-merge.py
82 lines (65 loc) · 2.55 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
from typing import List
from itertools import islice
from collections import defaultdict
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
email_to_name = {}
emails_set = set()
for account in accounts:
for email in islice(account, 1, len(account)):
email_to_name[email] = account[0]
emails_set.add(email)
email_to_pos = {k: v for v, k in enumerate(emails_set)}
pos_to_email = {k: v for k, v in enumerate(emails_set)}
emails = list(range(len(emails_set)))
count = [1] * len(emails)
def find_root(array, pos):
root = pos
while array[root] != root:
root = array[root]
while pos != root:
array[pos], pos = root, array[pos]
return root
for account in accounts:
for pos in range(2, len(account)):
cur, prev = account[pos - 1], account[pos]
cur_pos, prev_pos = email_to_pos[cur], email_to_pos[prev]
root_cur = find_root(emails, cur_pos)
root_prev = find_root(emails, prev_pos)
if root_cur != root_prev:
if count[root_cur] > count[root_prev]:
emails[root_cur] = emails[root_prev]
count[root_cur] += count[root_prev]
else:
emails[root_prev] = emails[root_cur]
count[root_prev] += count[root_cur]
tmp = defaultdict(list)
for pos in range(len(emails)):
root = find_root(emails, pos)
tmp[root].append(pos_to_email[pos])
return [[email_to_name[pos_to_email[k]]] + sorted(v) for k, v in tmp.items()]
class TestSolution:
def setup(self):
self.sol = Solution()
def test_case1(self):
assert sorted(
self.sol.accountsMerge(
[
["John", "[email protected]", "[email protected]"],
["John", "[email protected]"],
["John", "[email protected]", "[email protected]"],
["Mary", "[email protected]"],
]
)
) == sorted(
[
["Mary", "[email protected]"],
[
"John",
],
["John", "[email protected]"],
]
)