-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path721.accounts-merge.py
40 lines (36 loc) · 1.23 KB
/
721.accounts-merge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#
# @lc app=leetcode id=721 lang=python3
#
# [721] Accounts Merge
#
# @lc code=start
class Solution:
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
from collections import defaultdict
visited_accounts = [False] * len(accounts)
emails_accounts_map = defaultdict(list)
res = []
# Build up the graph.
for i, account in enumerate(accounts):
for j in range(1, len(account)):
email = account[j]
emails_accounts_map[email].append(i)
# DFS code for traversing accounts.
def dfs(i, emails):
if visited_accounts[i]:
return
visited_accounts[i] = True
for j in range(1, len(accounts[i])):
email = accounts[i][j]
emails.add(email)
for neighbor in emails_accounts_map[email]:
dfs(neighbor, emails)
# Perform DFS for accounts and add to results.
for i, account in enumerate(accounts):
if visited_accounts[i]:
continue
name, emails = account[0], set()
dfs(i, emails)
res.append([name] + sorted(emails))
return res
# @lc code=end