-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeAccounts.java
More file actions
48 lines (39 loc) · 1.53 KB
/
Copy pathMergeAccounts.java
File metadata and controls
48 lines (39 loc) · 1.53 KB
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
package Arrays;
import java.util.*;
/*
https://leetcode.com/problems/accounts-merge/discuss/1774749/JAVA-Union-Find
*/
public class MergeAccounts {
public List<List<String>> solution(List<List<String>> accounts) {
Map<String, Integer> emailMap = new HashMap<>();
UnionFind unionFind = new UnionFind(accounts.size());
for (var i = 0; i < accounts.size(); i++) {
List<String> emails = accounts.get(i);
for (var j = 1; j < emails.size(); j++) {
if (!emailMap.containsKey(emails.get(j))) {
emailMap.put(emails.get(j), i);
} else {
unionFind.union(j, emailMap.get(emails.get(j)));
}
}
}
Map<Integer, List<String>> accountMap = new HashMap<>();
for (Map.Entry<String, Integer> entry : emailMap.entrySet()) {
int accountId = entry.getValue();
int parentId = unionFind.find(accountId);
if (!accountMap.containsKey(parentId)) {
accountMap.put(parentId, new ArrayList<>());
}
accountMap.get(accountId).add(entry.getKey());
}
// final result
List<List<String>> result = new ArrayList<>();
for (Map.Entry<Integer, List<String>> entry : accountMap.entrySet()) {
List<String> emails = entry.getValue();
Collections.sort(emails);
emails.add(0, accounts.get(entry.getKey()).get(0));
result.add(emails);
}
return result;
}
}