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].

这个题目思路就是先将input list转换成为一个以email为key, 然后account index的一个graph, 然后对于每个account index, 将所有的email都append进入一个set, 同时把它的邻居id的email都append进入set, 最后将它的name和sorted(emails set) append进入ans.

1. Constraints

1) 如以上note

2) 只是需要注意的是, 对于account没有email的来说, 我的思路会将account作为独立的account append进入ans里面, 但是题目并没有提及, 并且也没有相关的test cases.

2. Ideas

DFS

1) 转换为email 为key, account id 为value的dictionary

2) def dfs function, get all emails that each email in ids of the d

3) for each account, dfs 一遍, 并将sorted 结果append进入ans

4) return ans

3. Code

 class Solution:
def MergeAccounts(self, accoutns):
ans, d, visited = [], collections.defaultdict(set). set()
for i , account in enumerate(accounts):
for j in range(1, len(account)):
d[account[j]].add(i)
def dfs(i, emails):
if i not in visited:
visited.add(i)
account = accounts[i]
for j in range(1, len(account)):
email = account[j]
emails.add(email)
for neig in d[email]:
dfs(neig, emails)
for i, account in enumerate(accounts):
if i not in visited:
name, emails = account[0], set()
dfs(i, emails)
ans.append([name] + sorted(emails))
return ans

4. Test cases

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"]]

[LeetCode] 721. Accounts Merge_Medium tag: DFS recursive的更多相关文章

  1. [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive

    Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...

  2. [LeetCode] 298. Binary Tree Longest Consecutive Sequence_Medium tag: DFS recursive

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  3. [LeetCode] 130. Surrounded Regions_Medium tag: DFS/BFS

    Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A reg ...

  4. [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  5. [LeetCode] 687. Longest Univalue Path_Easy tag: DFS recursive

    Given a binary tree, find the length of the longest path where each node in the path has the same va ...

  6. [LeetCode] 721. Accounts Merge 账户合并

    Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...

  7. [LeetCode] 100. Same Tree_Easy tag: DFS

    Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

  8. [LeetCode] 112. Path Sum_Easy tag: DFS

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  9. LeetCode 721. Accounts Merge

    原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each element accoun ...

随机推荐

  1. Esper学习之十三:EPL语法(九)

    距离上一篇博客已经有很多个月的时间了,由于工作的原因,没怎么腾出手来写博客.再加上自己已计划算法学习为第一要务,更新博客的事情暂时就放缓了脚步.所以各位童鞋大可不必等我的博客,先把文档看起来,有什么不 ...

  2. vue实现按需加载(懒加载)

    1.router文件中使用 export default new Router({ routes: [{ path: '/', name: 'Post', component: () => im ...

  3. 【JSP】EL函数和自定义EL函数

    简介 EL原本是JSTL1.0中的技术(所以EL和JSTL感情如此好就是自然的了),但是从JSP2.0开始,EL就分离出来纳入了JSP的标准了.但是EL函数还是和JSTL技术绑定在一起.下面将介绍如何 ...

  4. IIS7配置伪静态把后缀名映射为html方案

    1.在IIS新建站点.[创建的时候不用去选择版本和模式,默认即可] 2.选中站点,切换到功能试图,找到“处理程序映射",双击之后,在打开窗口右侧的操作栏目下做如下设置: 1)右边" ...

  5. 【CF839E】Mother of Dragons 折半状压

    [CF839E]Mother of Dragons 题意:给你一张n个点,m条边的无向图.你有k点能量,你可以把能量分配到任意一些点上,每个点分到的能量可以是一个非负实数.定义总能量为:对于所有边&l ...

  6. vue--自定义验证指令

    参考文档: https://cn.vuejs.org/v2/guide/custom-directive.html https://www.cnblogs.com/ilovexiaoming/p/68 ...

  7. 关于51单片机使用printf串口调试

    在51系列单片机上面使用串口的时候,有时候为了方便调试看一下输出结果,会用到printf函数输出到电脑终端,再用串口助手显示.但是单片机使用printf的时候有一点需要注意的地方. 1.首先添加头文件 ...

  8. CentOS6.5 下将 Python2.6.6 升级到Python3.5

    一. 从Python官网到获取Python3的包, 切换到目录/usr/local/src #wget https://www.python.org/ftp/python/3.5.1/Python-3 ...

  9. POJ-2336 Ferry Loading II(简单DP)

    Ferry Loading II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3763 Accepted: 1919 Desc ...

  10. 编译支持opengl的opencv

    opencv默认安装是不支持opengl的. 也就是如果调用一个支持opengl的窗口会报错,no opengl support ubuntu下安装opencv,支持opengl要在cmake的时候, ...