-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccountsMerge_721.cpp
67 lines (50 loc) · 1.74 KB
/
AccountsMerge_721.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
/*
~ Author : https://leetcode.com/tridib_2003/
~ Problem : 721. Accounts Merge
~ Link : https://leetcode.com/problems/accounts-merge/
*/
class Solution {
private:
unordered_map<string, vector<string> > adj;
unordered_map<string, bool> visited;
vector<string> merged;
void dfs(string currEmail) {
visited[currEmail] = true;
merged.emplace_back(currEmail);
for (auto adjEmail : adj[currEmail]) {
if (!visited[adjEmail])
dfs(adjEmail);
}
}
public:
vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
int n = accounts.size();
// creating graph where emails are nodes
for (auto curr : accounts) {
visited[curr[1]] = false;
for (int i = 1; i < curr.size() - 1; ++i) {
adj[curr[i]].emplace_back(curr[i + 1]);
adj[curr[i + 1]].emplace_back(curr[i]);
visited[curr[i + 1]] = false;
}
}
vector<vector<string> > res;
// initiate dfs from every person's first email
// if current node(email) is not visited and form the res
for (auto curr : accounts) {
if (visited[curr[1]])
continue;
merged.clear();
merged.emplace_back(curr[0]); // put the person name
// dfs on current component
dfs(curr[1]);
// sort emails
sort(merged.begin() + 1, merged.end());
res.emplace_back(merged);
}
return res;
}
};
// Time Complexity - O(nm log nm)
// Space Complexity - O(nm)
// where n = accounts.size() and m = accounts[i].size()