问题:
Given a list accounts
, each element accounts[i]
is a list of strings, where the first element accounts[i][0]
is a name, and the rest of the elements are emails representing emails of the account.
Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.
After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.
Example 1:
Input: accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]] Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]] Explanation: The first and third John's are the same person as they have the common email "johnsmith@mail.com". The second John and Mary are different people as none of their email addresses are used by other accounts. We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], ['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Note:
- The length of
accounts
will be in the range[1, 1000]
. - The length of
accounts[i]
will be in the range[1, 10]
. - The length of
accounts[i][j]
will be in the range[1, 30]
.
解决:
① 给了人名和他用的邮箱,一个人可能有多个邮箱,也可能重名。把同一个人的邮箱合并,当两个名字拥有相同的邮箱时,可以确定是同一个人。
并查集,本题的关键是将emai连接起来,将email进行分组,每一组都有一个代表或者parent。首先,设置每个email的parent为其本身,每个账户的email属于同一组,可以使用email列表中第一个email的parent。
例如:
a b c // b,c的parent为a
d e f // e,f的parent为d
g a d // abc,def合并为一个组g
class Solution { //120ms
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String,String> owner = new HashMap<>();
Map<String,String> parents = new HashMap<>();
Map<String,TreeSet<String>> unions = new HashMap<>();
for (List<String> a : accounts){
for (int i = 1;i < a.size();i ++){
parents.put(a.get(i),a.get(i));
owner.put(a.get(i),a.get(0));
}
}
for (List<String> a : accounts){
String tmp = find(a.get(1),parents);//将每个用户的第一个email作为parent
for (int i = 2;i < a.size();i ++){
parents.put(find(a.get(i),parents),tmp);
}
}
for (List<String> a : accounts){
String tmp = find(a.get(1),parents);
if (! unions.containsKey(tmp)) unions.put(tmp,new TreeSet<>());
for (int i = 1;i < a.size();i ++){
unions.get(tmp).add(a.get(i));
}
}
List<List<String>> res = new ArrayList<>();
for (String tmp : unions.keySet()){
List<String> emails = new ArrayList<>(unions.get(tmp));
emails.add(0,owner.get(tmp));
res.add(emails);
}
return res;
}
public String find(String s,Map<String,String> parents){
return parents.get(s) == s ? s : find(parents.get(s),parents);
}
}
② 在discuss中看到的。。
class Solution { //64ms
public List<List<String>> accountsMerge(List<List<String>> accounts) {
int n = accounts.size();
int[] map = new int[n];
for(int i = 0; i < n; i ++){
map[i] = i;
}
HashMap<String, Integer> parents = new HashMap<String, Integer>();
for(int i = 0; i < n; i ++){
List<String> list = accounts.get(i);
for(int j = 1; j < list.size(); j ++){
String email = list.get(j);
if(parents.containsKey(email)){
int p = parents.get(email);
int c = i;
int u = findRoot(map, p);
int v = findRoot(map, c);
if(u != v){
map[u] = v;
}
}else{
parents.put(email, i);
}
}
}
for(int i = 0; i < n; i ++){
map[i] = findRoot(map, map[i]);
}
HashMap<Integer, TreeSet<String>> accs = new HashMap<>();
for(int i = 0; i < n; i ++){
int root = map[i];
if(! accs.containsKey(root)){
accs.put(root, new TreeSet<String>());
}
List<String> list = accounts.get(i);
for(int j = 1; j < list.size(); j ++){
accs.get(root).add(list.get(j));
}
}
List<List<String>> res = new ArrayList<>();
for(Integer key : accs.keySet()){
String name = accounts.get(key).get(0);
List<String> list = new ArrayList<String>();
list.add(name);
list.addAll(accs.get(key));
res.add(list);
}
return res;
}
private int findRoot(int[] map, int v){
if(map[v] == v)return v;
map[v] = map[map[v]];
return findRoot(map, map[v]);
}
}