{"id":6182,"date":"2020-01-29T21:04:01","date_gmt":"2020-01-30T05:04:01","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6182"},"modified":"2020-01-29T21:04:21","modified_gmt":"2020-01-30T05:04:21","slug":"leetcode-721-accounts-merge","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/graph\/leetcode-721-accounts-merge\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 721. Accounts Merge"},"content":{"rendered":"\n<p>Given a list&nbsp;<code>accounts<\/code>, each element&nbsp;<code>accounts[i]<\/code>&nbsp;is a list of strings, where the first element&nbsp;<code>accounts[i][0]<\/code>&nbsp;is a&nbsp;<em>name<\/em>, and the rest of the elements are&nbsp;<em>emails<\/em>&nbsp;representing emails of the account.<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>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&nbsp;<strong>in sorted order<\/strong>. The accounts themselves can be returned in any order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><br><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> \naccounts = [[\"John\", \"johnsmith@mail.com\", \"john00@mail.com\"], [\"John\", \"johnnybravo@mail.com\"], [\"John\", \"johnsmith@mail.com\", \"john_newyork@mail.com\"], [\"Mary\", \"mary@mail.com\"]]\n<strong>Output:<\/strong> [[\"John\", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  [\"John\", \"johnnybravo@mail.com\"], [\"Mary\", \"mary@mail.com\"]]\n<strong>Explanation:<\/strong> \nThe first and third John's are the same person as they have the common email \"johnsmith@mail.com\".\nThe second John and Mary are different people as none of their email addresses are used by other accounts.\nWe could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'], \n['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong>The length of&nbsp;<code>accounts<\/code>&nbsp;will be in the range&nbsp;<code>[1, 1000]<\/code>.The length of&nbsp;<code>accounts[i]<\/code>&nbsp;will be in the range&nbsp;<code>[1, 10]<\/code>.The length of&nbsp;<code>accounts[i][j]<\/code>&nbsp;will be in the range&nbsp;<code>[1, 30]<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Union-Find<\/strong><\/h2>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {\n    unordered_map<string_view, int> ids;   \/\/ email to id\n    unordered_map<int, string_view> names; \/\/ id to name\n    vector<int> p(10000);\n    iota(begin(p), end(p), 0);\n    \n    function<int(int)> find = [&](int x) {\n      if (p[x] != x) p[x] = find(p[x]);\n      return p[x];\n    };\n    \n    auto getIdByEmail = [&](string_view email) {\n      auto it = ids.find(email);\n      if (it == ids.end()) {\n        int id = ids.size();\n        return ids[email] = id;\n      }\n      return it->second;\n    };\n\n    for (const auto& account : accounts) {      \n      int u = find(getIdByEmail(account[1]));      \n      for (int i = 2; i < account.size(); ++i) \n        p[find(u)] = find(getIdByEmail(account[i]));      \n      names[find(u)] = string_view(account[0]);\n    }\n\n    unordered_map<int, set<string>> mergered;\n    for (const auto& account : accounts)\n      for (int i = 1; i < account.size(); ++i) {\n        int id = find(getIdByEmail(account[i]));\n        mergered[id].insert(account[i]);\n      }    \n\n    vector<vector<string>> ans;\n    for (const auto& kv : mergered) {\n      ans.push_back({string(names[kv.first])});\n      ans.back().insert(ans.back().end(), kv.second.begin(), kv.second.end());\n    }\n\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a list&nbsp;accounts, each element&nbsp;accounts[i]&nbsp;is a list of strings, where the first element&nbsp;accounts[i][0]&nbsp;is a&nbsp;name, and the rest of the elements are&nbsp;emails&nbsp;representing emails of the account.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[76],"tags":[77,113],"class_list":["post-6182","post","type-post","status-publish","format-standard","hentry","category-graph","tag-graph","tag-union-find","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6182","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=6182"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6182\/revisions"}],"predecessor-version":[{"id":6184,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6182\/revisions\/6184"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}