-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL00721.java
50 lines (47 loc) · 1.68 KB
/
L00721.java
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
package leetcode;
import java.util.*;
import java.util.stream.*;
class Mail {
int[] parent;
public Mail(int size) {
this.parent = new int[size];
for (int i = 0; i < size; i++)
this.parent[i] = i;
}
public int find(int idx) {
if (this.parent[idx] != idx)
this.parent[idx] = this.find(this.parent[idx]);
return this.parent[idx];
}
public void union(int parent, int child) {
this.parent[this.find(child)] = this.find(parent);
}
}
public class L00721 {
public List<List<String>> accountsMerge(List<List<String>> accounts) {
int len = accounts.size();
Mail p = new Mail(len);
HashMap<String, Integer> mMap = new HashMap<>();
HashMap<Integer, List<String>> pMap = new HashMap<>();
List<List<String>> ans = new ArrayList<>();
for (int i = 0; i < len; i++) {
for (int j = 1; j < accounts.get(i).size(); j++)
if (mMap.containsKey(accounts.get(i).get(j)))
p.union(mMap.get(accounts.get(i).get(j)), i);
else
mMap.put(accounts.get(i).get(j), i);
}
for (String mail : mMap.keySet()) {
int root = p.find(mMap.get(mail));
List<String> mails = pMap.getOrDefault(root, new ArrayList<String>());
mails.add(mail);
pMap.put(root, mails);
}
for (int k : pMap.keySet()) {
List<String> name = Arrays.asList(accounts.get(k).get(0)), mails = pMap.get(k);
Collections.sort(mails);
ans.add(Stream.concat(name.stream(), mails.stream()).collect(Collectors.toList()));
}
return ans;
}
}