Account consolidation

Given a list of accounts accountsa c c o u n t s , each elementaccounts [i] accounts[i]a c c o u n t s [ i ] is a list of strings, where the first elementaccounts [i] [0] accounts[i][0]a c c o u n t s [ i ] [ 0 ] is the name (name), and the remaining elements are emails, which means the email address of the account.

Now, we want to merge these accounts. If two accounts have some common email addresses, the two accounts must belong to the same person. Please note that even if two accounts have the same name, they may belong to different people, because people may have the same name. A person can initially have any number of accounts, but all of their accounts have the same name.

After the accounts are merged, the accounts are returned in the following format: the first element of each account is the name, and the remaining elements are the email addresses in order. The account itself can be returned in any order.

Example 1:

enter:

accounts = [["John", "[email protected]", "[email protected]"], ["John", "[email protected]"], ["John", "[email protected]", "[email protected]"], ["Mary", "[email protected]"]]

Output:

[["John", '[email protected]', '[email protected]', '[email protected]'],  ["John", "[email protected]"], ["Mary", "[email protected]"]]
解释:
第一个和第三个 John 是同一个人,因为他们有共同的邮箱地址 "[email protected]"。 
第二个 John 和 Mary 是不同的人,因为他们的邮箱地址没有被其他帐户使用。
可以以任何顺序返回这些列表,例如答案 [['Mary''[email protected]']['John''[email protected]']['John''[email protected]''[email protected]''[email protected]']] 也是正确的。

prompt:

a c c o u n t s accounts The length of a c c o u n t s will be in[1, 1000] [1, 1000][ 1 , 1 0 0 0 ] .
accounts [i] accounts[i]a c c o u n t s [ i ] will be in length[1, 10] [1, 10][ 1 , 1 0 ] .
accounts [i] [j] accounts[i][j]a c c o u n t s [ i ] [ j ] will be in length[1, 30] [1, 30][ 1 , 3 0 ] .

class Solution {
    
    
public:
    int f[10000];
    int find(int x){
    
    
        return x==f[x]?x:f[x]=find(f[x]);
    }
    vector<vector<string>> accountsMerge(vector<vector<string>>& accounts) {
    
    
        int n=accounts.size();
        unordered_map<string,int>ma;
        for (int i=0;i<10000;i++){
    
    
            f[i]=i;
        }
        for (int i=0;i<n;i++){
    
    
            int m=accounts[i].size();
            for (int j=1;j<m;j++){
    
    
                if (ma.count(accounts[i][j])==0){
    
    
                    ma[accounts[i][j]]=i;
                }
                f[find(i)]=find(ma[accounts[i][j]]);
            }
        }
        unordered_map<int,vector<string> >ma1;
        for (auto &[str,i]:ma){
    
    
            ma1[find(i)].push_back(str);
        }
        vector<vector<string>>res;
        for (auto &[i,vec]:ma1){
    
    
            sort(vec.begin(),vec.end());
            vector<string>tmp;
            tmp.push_back(accounts[find(i)][0]);
            tmp.insert(tmp.end(),vec.begin(),vec.end());
            res.push_back(tmp);
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/weixin_43601103/article/details/112763190
Recommended