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:
accounts
will be in the range [1, 1000]
.accounts[i]
will be in the range [1, 10]
.accounts[i][j]
will be in the range [1, 30]
.1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | class Solution { int find(vector<int> &uf, int i) { while (i != uf[i]) i = uf[i]; return i; } public: vector<vector<string>> accountsMerge(vector<vector<string>> &accounts) { int sz = accounts.size(); vector<int> uf(sz, -1); map<string, int> mp; //email, name index; for (int i = 0; i < sz; ++i) { uf[i] = i; for (int j = 1; j < accounts[i].size(); ++j) { string email = accounts[i][j]; if (mp.find(email) == mp.end()) { mp[email] = i; } else { int i1 = find(uf, i); int i2 = find(uf, mp[email]); uf[i1] = i2; } } } map<int, set<string>> mp1; //name index, emails for (auto m : mp) { int i = find(uf, m.second); mp1[i].insert(m.first); } vector<vector<string>> ret; for (auto m : mp1) { vector<string> v; v.push_back(accounts[m.first][0]); vector<string> emails(m.second.begin(), m.second.end()); sort(emails.begin(), emails.end()); v.insert(v.end(), emails.begin(), emails.end()); ret.push_back(v); } return ret; } }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class Solution { int find(int[] uf, int i){ while(uf[i]!=i) i = uf[i]; return i; } public List<List<String>> accountsMerge(List<List<String>> accounts) { int sz = accounts.size(); int[] uf = new int[sz]; for(int i=0; i<sz; ++i) uf[i] = i; Map<String, Integer> map = new HashMap<>();//email, name index for(int i=0; i<sz; ++i){ for(int j=1; j<accounts.get(i).size(); ++j){ String email = accounts.get(i).get(j); if(!map.containsKey(email)){ map.put(email, i); }else{ int parent = find(uf, map.get(email)); int t = find(uf, uf[i]); uf[t] = parent; } } } Map<Integer, List<String>> map1 = new HashMap<>();//name index, emails for(Map.Entry<String, Integer> e : map.entrySet()){ String email = e.getKey(); int index = e.getValue(); int parent = find(uf, index); map1.computeIfAbsent(parent, k->new ArrayList<>()).add(email); } // System.out.println(Arrays.toString(uf)); List<List<String>> lists = new ArrayList<>(); for(Map.Entry<Integer, List<String>> e : map1.entrySet()){ String user = accounts.get(e.getKey()).get(0); List<String> list = new ArrayList<>(); list.add(user); Collections.sort(e.getValue()); list.addAll(e.getValue()); lists.add(list); } return lists; } } |
No comments:
Post a Comment