Skip to content

Latest commit

 

History

History
213 lines (195 loc) · 6.89 KB

721. Accounts Merge.md

File metadata and controls

213 lines (195 loc) · 6.89 KB

leetcode-cn Daily Challenge on January 18th, 2021.


Difficulty : Medium

Related Topics : DFSUnionFind


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 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 accounts[i][j] will be in the range [1, 30].

Solution

  • mine
    • Java
      • UnionFind Runtime: 548 ms, faster than 5.01%, Memory Usage: 250 MB, less than 5.08% of Java online submissions
        public List<List<String>> accountsMerge(List<List<String>> accounts) {
            int len = accounts.size();
            UF uf = new UF(len);
            for(int k = 0; k < len; k++){
                List<String> t = accounts.get(k);
                for(int i = 1; i < t.size(); i++){
                    uf.put(k, t.get(i));
                }
            }
        
            Map<String,Integer> map = new HashMap<>();
            for(int k = 0; k < len; k++){
                List<String> t = accounts.get(k);
                for(int i = 1; i < t.size(); i++){
                    String email = t.get(i);
                    if(map.containsKey(email)){
                        uf.union(uf.find(map.get(email)), k);
                    }else{
                        map.put(email, k);
                    }
                }
            }
            return uf.getRes(accounts);
        }
        
        class UF{
            int[] arr;
            int count;
            Map<Integer,Set<String>> eMap;
            public UF(int n){
                eMap = new HashMap<>();
                this.count = n;
                arr = new int[n];
                for(int i = 0; i < n; i++){
                    arr[i] = i;
                }
            }
        
            boolean union(int a, int b){
                if(find(a) != find(b)){
                    Set<String> setA = eMap.getOrDefault(find(a), new HashSet<>());
                    Set<String> setB = eMap.getOrDefault(find(b), new HashSet<>());
                    setB.addAll(setA);
                    eMap.put(find(b), setB);
                    arr[a] = find(b);
                    count--;
                }
                return false;
            }
        
            int find(int a){
                if(a != arr[a]){
                    arr[a] = find(arr[a]);
                }
                return arr[a];
            }
        
            void put(int i, String e){
                Set<String> set = eMap.getOrDefault(i, new HashSet<>());
                set.add(e);
                eMap.put(i, set);
            }
        
            List<List<String>> getRes(List<List<String>> accounts){
                List<List<String>> res = new ArrayList<>(count);
                for(int i = 0; i < arr.length; i++){
                    if(i != arr[i]) continue;
                    List<String> emails = new ArrayList<>(eMap.get(i));
                    Collections.sort(emails);
                    List<String> t = new ArrayList<>(emails.size() + 1);
                    t.add(accounts.get(i).get(0));
                    t.addAll(emails);
                    res.add(t);
                }
                return res;
            }
        }
        

  • the most votes
  • Runtime: 24 ms, faster than 96.60%, Memory Usage: 42.5 MB, less than 98.99% of Java online submissions
    public List<List<String>> accountsMerge(List<List<String>> accounts) {
        HashMap<String, User> map = new HashMap();
        List<User> allUser = new ArrayList();
    
        for (List<String> account : accounts) {
            User user = new User(account.get(0));
            allUser.add(user);
    
            for (int i = 1; i < account.size(); i++) {
                String email = account.get(i);
                if (map.containsKey(email)) {
                    User parent = findParent(user);
                    User existedParent = findParent(map.get(email));
                    parent.parent = existedParent;
                } else {
                    user.add(email);
                    map.put(email, user);
                }
            }
        }
    
        for (User user : allUser) {
            if (user.parent != user) {
                User parent = findParent(user);
                parent.addAll(user.emails);
            }
        }
    
        List<List<String>> result = new ArrayList();
        for (User user : allUser) {
            if (user.parent == user) {
                result.add(user.toList());
            }
        }
    
        return result;
    }
    
    User findParent(User user) {
        while (user.parent != user.parent.parent) {
            user.parent = user.parent.parent;
        }
        return user.parent;
    }
    
    class User {
    
        String name;
        User parent;
        List<String> emails;
    
        User(String name) {
            this.name = name;
            this.parent = this;
            emails = new ArrayList();
        }
    
        void add(String email) {
            emails.add(email);
        }
    
        void addAll(List<String> emails) {
            this.emails.addAll(emails);
        }
    
        List<String> toList() {
            List<String> result = new ArrayList();
            result.add(name);
            Collections.sort(emails);
            result.addAll(emails);
            return result;
        }
    }