Skip to content

Latest commit

 

History

History
126 lines (70 loc) · 4.07 KB

0721._Accounts_Merge.md

File metadata and controls

126 lines (70 loc) · 4.07 KB

721. Accounts Merge

难度: Medium

刷题内容

原题连接

内容描述

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", "[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]"]]
Explanation: 
The first and third John's are the same person as they have the common email "[email protected]".
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', '[email protected]'], ['John', '[email protected]'], 
['John', '[email protected]', '[email protected]', '[email protected]']] 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].

解题方案

思路 1 - 时间复杂度: O(len(accounts) * 最大的一个email列表长度 + emails最后的排序elge)- 空间复杂度: O(email的个数)******

union_find, 先把所有email第一次出现的idx存到字典里面,然后边遍历accounts和里面的email,如果当前email已经出现过了, 我们知道前面肯定有一个account和当前account是同一个了,然后我们union他们,然后找到uf列表里面所有的root account

最后将root account和其所有的email排序后的列表对应放到res,返回即可

beats 99.46%

class Solution:
    def accountsMerge(self, accounts):
        """
        :type accounts: List[List[str]]
        :rtype: List[List[str]]
        """
        uf = [i for i in range(len(accounts))]
        emails_lookup = {}
        
        def find(x):
            while x != uf[x]:
                uf[x] = uf[uf[x]]
                x = uf[x]
            return x

        def union(x1, x2):
            x1_root = find(x1)
            x2_root = find(x2)
            uf[x1_root] = x2_root

        for idx, pair in enumerate(accounts): # union 过程
            name, emails = pair[0], pair[1:]
            for email in emails:
                if email in emails_lookup:
                    union(idx, emails_lookup[email])
                    # break
                else:
                    emails_lookup[email] = idx
                
        final_account_idx = set(find(i) for i in uf) # 最后的 unique account idx
        final_account = [i for i in range(len(accounts))]
        
        for idx in final_account_idx: # 把那些 unique account 的邮件列表都放起来
            final_account[idx] = accounts[idx][1:]
            
        for i in set(range(len(accounts)))- final_account_idx: # 把所有邮件放到该放的位置
            final_account[find(i)].extend(accounts[i][1:])
                
        res = []
        for idx in final_account_idx: # res 中只要所有 unique account
            name = accounts[idx][0]
            account = [name] + sorted(list(set(final_account[idx])))
            res.append(account)
        return res