[LeetCode] 721. Accounts Merge_Medium tag: DFS recursive
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [LeetCode] 721. Accounts Merge 账户合并
Given a list accounts, each element accounts[i] is a list of strings, where the first element accoun ...
- [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 ...
- [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 ...
- LeetCode 721. Accounts Merge
原题链接在这里:https://leetcode.com/problems/accounts-merge/ 题目: Given a list accounts, each element accoun ...
随机推荐
- 关于使用Delphi XE10 进行android开发的一些总结
RAD,可以快速开发出来,但是问题较多最好别用 说实话 做出来的app 太!大!了! 十分的特别的占内存! FireMonkey 真心太大了... 太占内存了 开发一般应用还可 ...
- package.json字段全解(转)
Name 必须字段. 小提示: 不要在name中包含js, node字样: 这个名字最终会是URL的一部分,命令行的参数,目录名,所以不能以点号或下划线开头: 这个名字可能在require()方法中被 ...
- DIV高度自适应及注意问题(转)
本文和大家重点讨论一下DIV高度自适应及注意问题,主要包括父div高度随子div的高度改变而改变和子div高度随父亲div高度改变而改变两种情况. DIV高度自适应及注意问题 积累了一些经验,总结出一 ...
- CF510B Fox And Two Dots(搜索图形环)
B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Unity3D笔记 英保通四 虚拟轴应用及键盘事件
Input: 1.使用这个类能够读取输入管理器设置的按键,以及访问移动设备的多点触控或加速感应数据.想要读取轴向使用Input.GetAxis方法获取下列默认轴: "Horizontal&q ...
- CodeFrist、ModelFirst、DatabaseFirst
一.CodeFirst 使用System.Data.Entity.DbContext与System.Data.Entity.DbSet构建的数据模型,没有可视化文件但只有实体类的称为CodeFirst ...
- Docker学习计划三:Dockerfile 使用
我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dockerfile 是镜像和容器的关键,Dockerfile 可以非常容易的定义镜像内容 首先通过一张图来了解 Docker 镜像 ...
- node中非常重要的process对象,Child Process模块
node中非常重要的process对象,Child Process模块Child Process模块http://javascript.ruanyifeng.com/nodejs/child-proc ...
- &与&&, |与||区别
&和|称为短逻辑符,&&及||称为长逻辑符.长逻辑符只比较左边和右边的第一个元素,而短逻辑符会比较所有的 > a<-c(TRUE, FALSE, TRUE, FAL ...
- 《C与指针》——高级指针话题
指针真是让人又爱又恨..... 首先还是先来看一下C语言中的高级指针声明.不要被表面迷惑最重要. /* ** <C和指针>——高级指针话题 */ int i; //定义一个整型变量 int ...