资讯专栏INFORMATION COLUMN

[LeetCode] 721. Accounts Merge

lk20150415 / 271人阅读

Problem

Given a list accounts, each element accounts[i] is a list of strings, where the first element accountsi 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 accountsi will be in the range [1, 30].

Solution
class Solution {
    public List> accountsMerge(List> accounts) {
        //construct email graph
        Map parents = new HashMap<>();
        //save email-user relationships
        Map users = new HashMap<>();
        
        //initialize email graph, and save email-user map, btw
        for (List account: accounts) {
            for (int i = 1; i < account.size(); i++) {
                parents.put(account.get(i), account.get(i));
                users.put(account.get(i), account.get(0));
            }
        }
        
        //find parent and union to the first node"s parent in each group
        for (List account: accounts) {
            String parent = find(parents, account.get(1));
            for (int i = 2; i < account.size(); i++) {
                String curParent = find(parents, account.get(i));
                parents.put(curParent, parent);
            }
        }
        
        //loop through groups and save the unions
        Map> unions = new HashMap<>();
        for (List account: accounts) {
            String parent = find(parents, account.get(1));
            if (!unions.containsKey(parent)) {
                unions.put(parent, new TreeSet<>());
            }
            for (int i = 1; i < account.size(); i++) {
                unions.get(parent).add(account.get(i));
            }
        }
        
        //loop through unions map and save to result
        List> res = new ArrayList<>();
        for (String parent: unions.keySet()) {
            List emails = new ArrayList<>(unions.get(parent));
            emails.add(0, users.get(parent)); //put user name at start pos
            res.add(emails);
        }
        
        return res;
    }
    
    private String find(Map parents, String str) {
        if (!parents.containsKey(str)) return null;
        String parent = parents.get(str);
        if (parent.equals(str)) return parent;
        else return find(parents, parent);
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/72358.html

相关文章

  • 从零构建、部署去中心化Voting Dapp

    摘要:查看文件内容这个文件主要是封装了一个的供我们直接使用,可以从直接获取到对象供文件中使用。 如何编写智能合约(Smart Contract)- 从零构建和部署去中心化投票App,decentralization Voting Dapp 孔壹学院:国内区块链职业教育领先品牌 作者:黎跃春,区块链、高可用架构工程师微信:liyc1215 QQ群:348924182 博客:http://...

    mikasa 评论0 收藏0
  • [Leetcode] Merge Two Sorted Lists Merge K Sorted L

    摘要:注意因为堆中是链表节点,我们在初始化堆时还要新建一个的类。代码初始化大小为的堆拿出堆顶元素将堆顶元素的下一个加入堆中 Merge Two Sorted Lists 最新更新请见:https://yanjia.me/zh/2019/01/... Merge two sorted linked lists and return it as a new list. The new list...

    stefanieliang 评论0 收藏0
  • [LeetCode/LintCode] Merge Intervals

    摘要:方法上没太多难点,先按所有区间的起点排序,然后用和两个指针,如果有交集进行操作,否则向后移动。由于要求的,就对原数组直接进行操作了。时间复杂度是的时间。 Problem Given a collection of intervals, merge all overlapping intervals. Example Given intervals => merged intervals...

    gougoujiang 评论0 收藏0
  • [LintCode/LeetCode] Merge Sorted Array

    Problem Given two sorted integer arrays A and B, merge B into A as one sorted array. Notice You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements ...

    summerpxy 评论0 收藏0
  • [Leetcode] Merge Sorted Array 合并数组

    摘要:但是如果我们从后往前,合并到第一个数组的最后,则不用位移。注意将和都先减,用和来代表下标,避免两个数组为空时抛出空指针异常。 Merge Sorted Array 最新更新请见:https://yanjia.me/zh/2019/02/... Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1...

    quietin 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<