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. 原生js--兼容获取窗口滚动条位置和窗口大小的方法

    各个浏览器对获取获取窗口滚动条位置和窗口大小没有提供统一的API,以下是对其封装,解决兼容性问题 /** * 获取浏览器视口的大小(显示文档的部分) *  */function getViewPort ...

  2. Ubuntu 最好用的CHM阅读器KchmViewer

    直接在“ubuntu软件中心”进行搜索安装 为什么说它是最好用?很简单!可同时显示目录和内容,中文没乱码!能实现这两点的竞争对手已经不多了,至少我是没发现.什么chmsee,gnochm,都有乱码.虽 ...

  3. 第二步 使用Cordova 3.0(及以上版本) 创建安卓项目(2014-6-25)

    参考资料: http://www.cnblogs.com/numtech/p/3233469.html http://blog.sina.com.cn/s/blog_9e245c690101jurr. ...

  4. JavaScript 异步进化史

    前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...

  5. Cracking the Coding Interview(linked list)

    第二章的内容主要是关于链表的一些问题. 基础代码: class LinkNode { public: int linknum; LinkNode *next; int isvisit; protect ...

  6. 【咸鱼教程】protobuf在websocket通讯中的使用

    教程目录一 protobuf简介二 使用protobuf三 Demo下载 参考: CSDN:Egret项目中使用protobuf(protobufjs) TS项目中使用Protobuf的解决方案(ba ...

  7. vs 的git插件

    在vs2013上Tfs提供的git管理完全无法理解他的管理方式,还是 Git Source Control Provider 好用用. 下载地址: [http://gitscc.codeplex.co ...

  8. Nhibernate学习

    Contains 缓存中是否存在SessionFactoryBizCom.GetInstance().GetCurrentSession().Contains(t1) Evict 临时状态(Trans ...

  9. Unity3D笔记 愤怒的小鸟<六> 弹弓发射小鸟

    要实现的目标 实现个性化的鼠标 实现弹弓 选择小鸟.拉升弹弓.发射小鸟 弹弓橡皮筋 声音 1.实现个性化鼠标 效果 2.添加弹弓 建立两个材质 创建一个空GameObject 把两个shoot拖进来统 ...

  10. windows乱码

    对于支持 UNICODE的应用程序,Windows 会默认使用 Unicode编码.对于不支持Unicode的应用程序Windows 会采用 ANSI编码 (也就是各个国家自己制定的标准编码方式,如对 ...