(算法分析Week16)Accounts Merge[Medium]

来源:互联网 发布:刘国梁怎么了知乎 编辑:程序博客网 时间:2024/06/06 02:49

721. Accounts Merge[Medium]

题目来源

Description

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

给了人名和他用的邮箱,一个人可能有多个邮箱,也可能重名。题目要求是,把同一个人的邮箱合并,当两个名字拥有相同的邮箱时,可以确定是同一个人。
本来想用一个map存用户名和对应的邮箱地址,然后遍历,在map中找,如果邮箱地址有相交部分,就把剩下的都加上去。但是有一个问题,比如[a, c]、[b, e], [e, c]这样的顺序,[b, e]会被认为不是同一个人的邮箱,然后WA。其实可以重复这样的操作两次,就不会有遗漏,利用set排序,能过,但是复杂度还是太高了。
一个应该比较正统的做法:归组。具体的思路可以看这个链接

Complexity analysis

用了递归不好估计,但是这个递归可以转化成循环,所以复杂度应该是
O(n²)

Code

*这是按照归组的思路写的

class Solution {public:    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {        vector<vector<string>> result;        unordered_map<string, string> root;        unordered_map<string, string> owner;        unordered_map<string, set<string>> m;        int size = accounts.size();        for (auto account : accounts) {            for (int i = 1; i < account.size(); ++i) {                root[account[i]] = account[i];                owner[account[i]] = account[0];            }        }        for (auto account : accounts) {            string p = find(account[1], root);            for (int i = 2; i < account.size(); ++i) {                root[find(account[i], root)] = p;            }        }        for (auto account : accounts) {            for (int i = 1; i < account.size(); ++i) {                m[find(account[i], root)].insert(account[i]);            }        }        for (auto a : m) {            vector<string> v(a.second.begin(), a.second.end());            v.insert(v.begin(), owner[a.first]);            result.push_back(v);        }        return result;    }    string find(string s, unordered_map<string, string>& root) {        return root[s] == s ? s : find(root[s], root);    }};

Result

这里写图片描述