721.账户合并

  • 并查集

给定一个列表 accounts,每个元素 accounts[i] 是一个字符串列表,其中第一个元素 accounts[i][0] 是 名称 (name),其余元素是 emails 表示该帐户的邮箱地址。
现在,我们想合并这些帐户。如果两个帐户都有一些共同的邮件地址,则两个帐户必定属于同一个人。请注意,即使两个帐户具有相同的名称,它们也可能属于不同的人,因为人们可能具有相同的名称。一个人最初可以拥有任意数量的帐户,但其所有帐户都具有相同的名称。
合并帐户后,按以下格式返回帐户:每个帐户的第一个元素是名称,其余元素是按顺序排列的邮箱地址。accounts 本身可以以任意顺序返回。
例子 1:
Input:
accounts = [[“John”, “[email protected]”, “[email protected]”], [“John”, “[email protected]”], [“John”, “[email protected]”, “[email protected]”], [“Mary”, “[email protected]”]]
Output: [[“John”, ‘[email protected]’, ‘[email protected]’, ‘[email protected]’], [“John”, “[email protected]”], [“Mary”, “[email protected]”]]
Explanation:
第一个和第三个 John 是同一个人,因为他们有共同的电子邮件 “[email protected]”。
第二个 John 和 Mary 是不同的人,因为他们的电子邮件地址没有被其他帐户使用。
我们可以以任何顺序返回这些列表,例如答案[[‘Mary’,‘[email protected]’],[‘John’,‘[email protected]’],
[‘John’,‘[email protected]’,‘[email protected]’,‘[email protected]’]]仍然会被接受。
注意:
accounts的长度将在[1,1000]的范围内。
accounts[i]的长度将在[1,10]的范围内。
accounts[i][j]的长度将在[1,30]的范围内。

维护一个map,key是每个邮箱的名字,value是这个邮箱出现在哪些人的名下,然后得到了这些有交集的人就可以用并查集来进行聚类。

class Solution {
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        int N = accounts.size();

        //初始化每个节点的代表
        int[] boss = new int[N];
        for (int i = 0;i < N;i++){
            boss[i] = i;
        }

        //每个邮箱对应的人
        Map<String,List<Integer>> map = new HashMap<String,List<Integer>>();
        for (int i = 0;i < N;i++){
            List<String> emails = accounts.get(i);
            int len = emails.size();
            for (int j = 1;j < len;j++){
                String email = emails.get(j);
                if (map.containsKey(email)){
                    map.get(email).add(i);
                } else {
                    List<Integer> temp = new ArrayList<>();
                    temp.add(i);
                    map.put(email,temp);
                }
            }
        }

        //对每个邮箱对用的人进行聚类
        for (List<Integer> persons:map.values()){
            //找到boss最小的那个
            int min = Integer.MAX_VALUE;
            for (int person:persons){
                min = Math.min(min,boss[person]);
            }

            //聚类
            for (int person:persons){
                if (boss[person] != min){
                    int target = boss[person];
                    for (int i = 0;i < N;i++){
                        if (boss[i] == target){
                            boss[i] = min;
                        }
                    }
                }
            }
        }

        //对邮箱进行整理
        for (int i = 0;i < N;i++){
            List<String> b = accounts.get(i);
            if (boss[i] != i){
                List<String> a = accounts.get(boss[i]);
                for (String email:b){
                    if (!a.contains(email)){
                        a.add(email);
                    }
                }
            } else { //本来的邮箱里面也有重复邮箱
                List<String> a = new ArrayList<>();
                for (String temp:b){
                    if (!a.contains(temp)){
                        a.add(temp);
                    }
                }
                accounts.get(i).clear();
                accounts.get(i).addAll(a);
            }
        }
        List<List<String>> ans = new ArrayList<List<String>>();
        for (int i = 0;i < N;i++){
            if (boss[i] == i){
                List<String> emails = accounts.get(i).subList(1,accounts.get(i).size());
                String name = accounts.get(i).get(0);
                Collections.sort(emails);
                emails.add(0,name);
                ans.add(emails);
            }
        }
        return ans;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章