721. Accounts Merge
721. Accounts Merge Union find https://www.youtube.com/watch?v=VJnUwsE4fWA https://www.youtube.com/watch?v=ID00PMy0-vE&t=572s Uf code in java http://zxi.mytechroad.com/blog/data-structure/sp1-union-find-set/ 这个把图画一下,不然很快就忘了 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. class Solution { public List<List<String>> accountsMerge(List<List<String>> accounts) { UnionFind uf = new UnionFind(); Map<String, String> emailToName = new HashMap<>(); Map<String, Integer> emailToID = new HashMap<>(); int id = 0; // later use for increment the id for(List<String> account : accounts){ for(int i = 1; i < account.size(); i++){ if(!emailToName.containsKey(account.get(i))){ emailToName.put(account.get(i), account.get(0)); emailToID.put(account.get(i), id++); int firstEmail = emailToID.get(account.get(1)); // the first email's id int other = emailToID.get(account.get(i)); // other emails id from the same account uf.union(firstEmail, other); // in the same account, we union the first email with the rest } } } Map<Integer, List<String>> ans = new HashMap<>(); // key: ultimate root,val: the list of emails belong to that root for(String email : emailToID.keySet()){ //get all the distinct email,then get their id, then get their ultimate root id int root = uf.find(emailToID.get(email)); if(!ans.containsKey(root)){ ans.put(root, new ArrayList<String>()); //// } ans.get(root).add(email); } for(List<String> emails: ans.values()){ // grouped unsorted emails without names Collections.sort(emails); emails.add(0, emailToName.get(emails.get(0))); } return new ArrayList(ans.values()); /////// } class UnionFind{ int[] id; public UnionFind(){ id = new int[10001]; for(int i = 0; i <= 10000l; i++){ id[i] = i; } } public int find(int x){ while(id[x] != x){ x = id[x]; } return x; } public void union(int p, int q){ int proot = find(p); int qroot = find(q); id[qroot] = proot; } } } ======================================================================================================= email to id hashmap email: "johnsmith@mail.com", "john00@mail.com", "johnnybravo@mail.com", "john_newyork@mail.com", "mary@mail.com" id : 0 1 2 3 4 =========================================================================================================== email to name hashmap email: "johnsmith@mail.com", "john00@mail.com", "johnnybravo@mail.com", "john_newyork@mail.com", "mary@mail.com" name: john john john john mary =========================================================================================================== int[] id: 0 0 2 0 4 index. 0. 1. 2 3. 4 =========================================================================================================== ans hashmap ultimate root 0 2 4 emails "johnsmith@mail.com" "johnnybravo@mail.com "mary@mail.com" "john00@mail.com" john_newyork@mail.com
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.
posted on 2018-08-09 18:59 猪猪🐷 阅读(128) 评论(0) 收藏 举报
浙公网安备 33010602011771号