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. 【前端积累】Awesome初识

    前言 之所以要看这个,是因为在看到的一个网站里图表显示的全屏和缩小,anyway ,还是看一下咯~ 一.介绍 Font Awesome 字体为您提供可缩放矢量图标,它可以被定制大小.颜色.阴影以及任何 ...

  2. OGG日常运维监控的自动化脚本模板

    #!/usr/bin/ksh export ORACLE_BASE=/oracle/ export ORACLE_SID=epmln1 export ORACLE_HOSTNAME=pmlnpdb1 ...

  3. tcp连接的状态变迁以及如何调整tcp连接中处于time_wait的时间

    一.状态变迁图 二.time_wait状态 针对time_wait和close_wait有个简单的描述帮助理解: Due to the way TCP/IP works, connections ca ...

  4. 4G厂商版《出师表》

    转载:http://mp.weixin.qq.com/s?__biz=MzAwNDAyODM0NA==&mid=408013599&idx=1&sn=70cd33d037604 ...

  5. shell 中的$0 $1 $* $@ $# $$ $? $() $(())

    $0: 脚本本身文件名称 : 命令行第一个参数,$2为第二个,以此类推 $*: 所有参数列表 $@: 所有参数列表 $#: 参数个数 $$: 脚本运行时的PID $?: 脚本退出码 ∗与@的区别 当命 ...

  6. 【CF708D】Incorrect Flow 最小费用可行流

    [CF708D]Incorrect Flow 题意:给你一个点数为n,边数为m的流网络,每条边有一个容量c和流量f,这个网络可能是不合法的.你可以花费1的代价使c或f减少或增加1,可以修改无限次.你不 ...

  7. iOS - 开源框架、项目和学习资料汇总(其他篇)

    数据库 1. FMDB – sqlite的工具, 多线程FMDatabaseQueue实例,FMDB数据库的使用演示和封装工具类.GXDatabaseUtils – 在FMDB基础上的工具.2. re ...

  8. EDA优势

    1.提供明确的表述性业务概念 在某些场景下,一个业务概念会被多个流程更改,如果此属性逻辑发生变化,其他关联的流程将无法知晓,导致bug产生 如:出于性能或其他因素考虑下,为A表增加一个冗余字段,操作A ...

  9. ubuntu16.04下安装kdevelop和汉化

    1.Kdevelop安装 最简单的命令行安装,打开终端,执行 sudo apt-get install kdevelop 2.Kdevelop汉化 不需要自己下载汉化包,Kdevelop安装后,在终端 ...

  10. UOJ 145 - 神奇的幻方 - [简单数学题]

    题目链接:http://uoj.ac/problem/145 题目描述 幻方是一种很神奇的 N∗N 矩阵:它由数字 1,2,3,⋯⋯,N×N 构成,且每行.每列及两条对角线上的数字之和都相同. 当 N ...