721. 账户合并
难度:中等
给定一个列表 accounts
,每个元素 accounts[i]
是一个字符串列表,其中第一个元素 accounts[i][0]
是 名称 (name),其余元素是 emails 表示该账户的邮箱地址。
现在,我们想合并这些账户。如果两个账户都有一些共同的邮箱地址,则两个账户必定属于同一个人。请注意,即使两个账户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的账户,但其所有账户都具有相同的名称。
合并账户后,按以下格式返回账户:每个账户的第一个元素是名称,其余元素是按字符 ASCII 顺序排列的邮箱地址。账户本身可以以任意顺序返回。
示例 1:
输入:
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
输出:
[["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
解释:
第一个和第三个 John 是同一个人,因为他们有共同的邮箱地址 "johnsmith@mail.com"。
第二个 John 和 Mary 是不同的人,因为他们的邮箱地址没有被其他帐户使用。
可以以任何顺序返回这些列表,例如答案 [['Mary','mary@mail.com'],['John','johnnybravo@mail.com'],
['John','john00@mail.com','john_newyork@mail.com','johnsmith@mail.com']] 也是正确的。
提示:
accounts
的长度将在[1,1000]
的范围内。accounts[i]
的长度将在[1,10]
的范围内。accounts[i][j]
的长度将在[1,30]
的范围内。
解答:
class UnionFind{
int[] parent;
public UnionFind(int n){
parent = new int[n];
for(int i = 0; i < n; i++){
parent[i] = i;
}
}
public int find(int index){
if(parent[index] != index){
parent[index] = find(parent[index]);
}
return parent[index];
}
public void union(int x, int y){
parent[find(y)] = find(x);
}
}
class Solution {
//哈希表+并查集
//时间复杂度O(NlogN),空间复杂度O(N)
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String, Integer> emailToIndex = new HashMap<>();
Map<String, String> emailToName = new HashMap<>();
int emailsCount = 0;
for(List<String> account : accounts){
String name = account.get(0);
for(int i = 1; i < account.size(); i++){
String email = account.get(i);
if(!emailToIndex.containsKey(email)){
emailToIndex.put(email, emailsCount++);
emailToName.put(email, name);
}
}
}
UnionFind uf = new UnionFind(emailsCount);
for(List<String> account : accounts){
String firstEmail = account.get(1);
int firstIndex = emailToIndex.get(firstEmail);
for(int i = 2; i < account.size(); i++){
String nextEmail = account.get(i);
int nextIndex = emailToIndex.get(nextEmail);
uf.union(firstIndex, nextIndex);
}
}
Map<Integer, List<String>> indexToEmails = new HashMap<>();
for(String email : emailToIndex.keySet()){
int index = uf.find(emailToIndex.get(email));
List<String> account = indexToEmails.getOrDefault(index, new ArrayList<>());
account.add(email);
indexToEmails.put(index, account);
}
List<List<String>> merged = new ArrayList<>();
for(List<String> emails : indexToEmails.values()){
Collections.sort(emails);
String name = emailToName.get(emails.get(0));
List<String> account = new ArrayList<>();
account.add(name);
account.addAll(emails);
merged.add(account);
}
return merged;
}
}
参考自:
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/accounts-merge/solution/zhang-hu-he-bing-by-leetcode-solution-3dyq/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。