-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
accounts-merge.cpp
63 lines (57 loc) · 2.06 KB
/
accounts-merge.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
// Time: O(nlogn), n is the number of total emails, and the max length of email is 320, p.s. {64}@{255}
// Space: O(n)
class Solution {
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
UnionFind union_find;
unordered_map<string, string> email_to_name;
unordered_map<string, int> email_to_id;
for (const auto& account : accounts) {
const auto& name = account[0];
for (int i = 1; i < account.size(); ++i) {
if (!email_to_id.count(account[i])) {
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]]);
}
}
unordered_map<int, set<string>> lookup;
for (const auto& kvp : email_to_name) {
const auto& email = kvp.first;
lookup[union_find.find_set(email_to_id[email])].emplace(email);
}
vector<vector<string>> result;
for (const auto& kvp : lookup) {
const auto& emails = kvp.second;
vector<string> tmp{email_to_name[*emails.begin()]};
for (const auto& email : emails) {
tmp.emplace_back(email);
}
result.emplace_back(move(tmp));
}
return result;
}
private:
class UnionFind {
public:
int get_id() {
set_.emplace_back(set_.size());
return set_.size() - 1;
}
int find_set(const int x) {
if (set_[x] != x) {
set_[x] = find_set(set_[x]); // Path compression.
}
return set_[x];
}
void union_set(const int x, const int y) {
int x_root = find_set(x), y_root = find_set(y);
if (x_root != y_root) {
set_[min(x_root, y_root)] = max(x_root, y_root);
}
}
private:
vector<int> set_;
};
};