Accounts Merge
Description
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
Example 1:
Input:
[
["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.
You 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']
]
is also acceptable.
解题思路: 使用并查集。1. 建立 Email -> List[User] 关系。 2. 将属于同一Email的user 使用并查集连接。 3. 建立User -> List[Email] 4. 根据并查集 找到每个User的老大哥,以老大哥为代表输出旗下所以Email。
public class Solution {
/**
* @param accounts: List[List[str]]
* @return: return a List[List[str]]
*/
int[] father;
public List<List<String>> accountsMerge(List<List<String>> accounts) {
father = new int[accounts.size()];
for (int i = 0; i < accounts.size(); i++) {
father[i] = i;
}
//union
Map<String, List<Integer>> emailToIds = getEmailToIds(accounts);
for (List<Integer> ids: emailToIds.values()) {
for (int i = 1; i < ids.size(); i++) {
union(ids.get(i), ids.get(0));
}
}
//merge
Map<Integer, Set<String>> idToEmailSet = getIdToEmailSet(accounts);
List<List<String>> mergedAccounts = new ArrayList<>();
for (Map.Entry<Integer, Set<String>> entry : idToEmailSet.entrySet()) {
Integer userId = entry.getKey();
List<String> emails = new ArrayList(entry.getValue());
Collections.sort(emails);
emails.add(0, accounts.get(userId).get(0));
mergedAccounts.add(emails);
}
return mergedAccounts;
}
private Map<String, List<Integer>> getEmailToIds(List<List<String>> accounts) {
Map<String, List<Integer>> emailToIds = new HashMap<>();
for (int userId = 0; userId < accounts.size(); userId++) {
List<String> account = accounts.get(userId);
for (int i = 1; i < account.size(); i++) {
List<Integer> ids = emailToIds.getOrDefault(account.get(i), new ArrayList<Integer>());
ids.add(userId);
emailToIds.put(account.get(i), ids);
}
}
return emailToIds;
}
private Map<Integer, Set<String>> getIdToEmailSet(List<List<String>> accounts) {
Map<Integer, Set<String>> idToEmailSet = new HashMap<>();
for (int userId = 0; userId < accounts.size(); userId++) {
List<String> account = accounts.get(userId);
int root = find(userId);
Set<String> emails = idToEmailSet.getOrDefault(root, new HashSet<String>());
for (int i = 1; i < account.size(); i++) {
emails.add(account.get(i));
}
idToEmailSet.put(root, emails);
}
return idToEmailSet;
}
private void union (int id1, int id2) {
int root1 = find(id1);
int root2 = find(id2);
if (root1 != root2) {
father[root1] = root2;
}
}
private int find(int id) {
int node = id;
while (father[node] != node) {
node = father[node];
}
int root = node;
while (id != root) {
int tmp = father[id];
father[id] = root;
id = tmp;
}
return root;
}
}
Accounts Merge的更多相关文章
- 【LeetCode】721. Accounts Merge 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/accounts ...
- [LeetCode] Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [Swift]LeetCode721. 账户合并 | Accounts Merge
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [leetcode]721. Accounts Merge账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- 721. Accounts Merge合并电子邮件账户
[抄题]: Given a list accounts, each element accounts[i] is a list of strings, where the first element ...
- [LeetCode] 721. Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- LeetCode 721. Accounts Merge
原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each element accoun ...
- 【leetcode】721. Accounts Merge(账户合并)
Given a list of accounts where each element accounts[i] is a list of strings, where the first elemen ...
- 721. Accounts Merge
https://leetcode.com/problems/accounts-merge/description/ class UnionFound { public: unordered_map&l ...
随机推荐
- 032 Android智能下拉刷新框架-SmartRefreshLayout+RecyclerView的使用
1.SmartRefreshLayout介绍 SmartRefreshLayout的目标是打造一个强大,稳定,成熟的下拉刷新框架,并集成各种的炫酷.多样.实用.美观的Header和Footer. 正如 ...
- Java线程同步synchronized的理解
JVM中(留神:马上讲到的这两个存储区只在JVM内部与物理存储区无关)存在一个主内存(Main Memory),Java中所有的变量存储在主内存中,所有实例和实例的字段都在此区域,对于所有的线程是共享 ...
- 使用 Vagrant + VirtualBox 快速构建 CentOS 下的 Docker 环境
Vagrant - 基础概念: Vagrant 是什么? Vagrant是一款用于在单个工作流程中构建和管理虚拟机环境的工具.凭借易于使用的工作流程和专注于自动化,Vagrant降低了开发环境设置时间 ...
- 100天搞定机器学习|day54 聚类系列:层次聚类原理及案例
几张GIF理解K-均值聚类原理 k均值聚类数学推导与python实现 前文说了k均值聚类,他是基于中心的聚类方法,通过迭代将样本分到k个类中,使每个样本与其所属类的中心或均值最近. 今天我们看一下无监 ...
- svn: E230001: Server SSL certificate verification failed: certificate issued
svn: E230001: Server SSL certificate verification failed: certificate issued 今天在使用svn时候发现出现这个问题,这个是因 ...
- dotnet Core学习之旅(二):安装IDE
[重要:文中所有外链不能确保永久有效] >开发工具 高效的开发必然需要一个优秀的集成开发环境(IDE) 对于.NET Core 2.x可以使用包括但不限于以下IDE来进行开发. Visual S ...
- App功能测试点总结
1.手机操作系统android(谷歌).ios(苹果).Windows phone(微软).Symbian(诺基亚).BlackBerry OS(黑莓).windows mobile(微软),目前主流 ...
- 在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存)
原文:在论坛中出现的比较难的sql问题:40(子查询 销售和历史库存) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有 ...
- 在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题)
原文:在论坛中出现的比较难的sql问题:39(动态行转列 动态日期列问题) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉 ...
- VS2015编译Teamtalk的Windows客户端(转)
原文链接:https://blog.csdn.net/qtstar/article/details/54732581 一.(首先要把teamtalk整个项目download下来或git一个副本下来)打 ...