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;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章