Skip to content

Commit 0ef84c5

Browse files
committed
Sync LeetCode submission Runtime - 30 ms (52.74%), Memory - 26.1 MB (8.35%)
1 parent 9fcc7fa commit 0ef84c5

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

0721-accounts-merge/README.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<p>Given a list of <code>accounts</code> where each element <code>accounts[i]</code> is a list of strings, where the first element <code>accounts[i][0]</code> is a name, and the rest of the elements are <strong>emails</strong> representing emails of the account.</p>
2+
3+
<p>Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some common email 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.</p>
4+
5+
<p>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 <strong>in sorted order</strong>. The accounts themselves can be returned in <strong>any order</strong>.</p>
6+
7+
<p>&nbsp;</p>
8+
<p><strong class="example">Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong> accounts = [[&quot;John&quot;,&quot;johnsmith@mail.com&quot;,&quot;john_newyork@mail.com&quot;],[&quot;John&quot;,&quot;johnsmith@mail.com&quot;,&quot;john00@mail.com&quot;],[&quot;Mary&quot;,&quot;mary@mail.com&quot;],[&quot;John&quot;,&quot;johnnybravo@mail.com&quot;]]
12+
<strong>Output:</strong> [[&quot;John&quot;,&quot;john00@mail.com&quot;,&quot;john_newyork@mail.com&quot;,&quot;johnsmith@mail.com&quot;],[&quot;Mary&quot;,&quot;mary@mail.com&quot;],[&quot;John&quot;,&quot;johnnybravo@mail.com&quot;]]
13+
<strong>Explanation:</strong>
14+
The first and second John&#39;s are the same person as they have the common email &quot;johnsmith@mail.com&quot;.
15+
The third John and Mary are different people as none of their email addresses are used by other accounts.
16+
We could return these lists in any order, for example the answer [[&#39;Mary&#39;, &#39;mary@mail.com&#39;], [&#39;John&#39;, &#39;johnnybravo@mail.com&#39;],
17+
[&#39;John&#39;, &#39;john00@mail.com&#39;, &#39;john_newyork@mail.com&#39;, &#39;johnsmith@mail.com&#39;]] would still be accepted.
18+
</pre>
19+
20+
<p><strong class="example">Example 2:</strong></p>
21+
22+
<pre>
23+
<strong>Input:</strong> accounts = [[&quot;Gabe&quot;,&quot;Gabe0@m.co&quot;,&quot;Gabe3@m.co&quot;,&quot;Gabe1@m.co&quot;],[&quot;Kevin&quot;,&quot;Kevin3@m.co&quot;,&quot;Kevin5@m.co&quot;,&quot;Kevin0@m.co&quot;],[&quot;Ethan&quot;,&quot;Ethan5@m.co&quot;,&quot;Ethan4@m.co&quot;,&quot;Ethan0@m.co&quot;],[&quot;Hanzo&quot;,&quot;Hanzo3@m.co&quot;,&quot;Hanzo1@m.co&quot;,&quot;Hanzo0@m.co&quot;],[&quot;Fern&quot;,&quot;Fern5@m.co&quot;,&quot;Fern1@m.co&quot;,&quot;Fern0@m.co&quot;]]
24+
<strong>Output:</strong> [[&quot;Ethan&quot;,&quot;Ethan0@m.co&quot;,&quot;Ethan4@m.co&quot;,&quot;Ethan5@m.co&quot;],[&quot;Gabe&quot;,&quot;Gabe0@m.co&quot;,&quot;Gabe1@m.co&quot;,&quot;Gabe3@m.co&quot;],[&quot;Hanzo&quot;,&quot;Hanzo0@m.co&quot;,&quot;Hanzo1@m.co&quot;,&quot;Hanzo3@m.co&quot;],[&quot;Kevin&quot;,&quot;Kevin0@m.co&quot;,&quot;Kevin3@m.co&quot;,&quot;Kevin5@m.co&quot;],[&quot;Fern&quot;,&quot;Fern0@m.co&quot;,&quot;Fern1@m.co&quot;,&quot;Fern5@m.co&quot;]]
25+
</pre>
26+
27+
<p>&nbsp;</p>
28+
<p><strong>Constraints:</strong></p>
29+
30+
<ul>
31+
<li><code>1 &lt;= accounts.length &lt;= 1000</code></li>
32+
<li><code>2 &lt;= accounts[i].length &lt;= 10</code></li>
33+
<li><code>1 &lt;= accounts[i][j].length &lt;= 30</code></li>
34+
<li><code>accounts[i][0]</code> consists of English letters.</li>
35+
<li><code>accounts[i][j] (for j &gt; 0)</code> is a valid email.</li>
36+
</ul>

0721-accounts-merge/solution.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Approach 1: Depth First Search
2+
3+
# n = no. of accounts, k = max length of account
4+
# Time: O(n * k * log (nk))
5+
# Space: O(nk)
6+
7+
class Solution:
8+
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
9+
# create email to name mapping
10+
email_to_name = {}
11+
# create email graph
12+
email_graph = {}
13+
14+
# Build the graph
15+
for account in accounts:
16+
name = account[0]
17+
# process all emails in the account
18+
for email in account[1:]:
19+
email_to_name[email] = name
20+
if email not in email_graph:
21+
email_graph[email] = set()
22+
23+
# connect first email with all other emails in the account
24+
if len(account) > 2:
25+
email_graph[account[1]].add(email)
26+
email_graph[email].add(account[1])
27+
28+
# DFS function to find all connected emails
29+
def dfs(email, visited, connected_emails):
30+
visited.add(email)
31+
connected_emails.append(email)
32+
for neighbor in email_graph[email]:
33+
if neighbor not in visited:
34+
dfs(neighbor, visited, connected_emails)
35+
36+
# Process all emails and merge accounts
37+
visited = set()
38+
merged_accounts = []
39+
40+
for email in email_graph:
41+
if email not in visited:
42+
connected_emails = []
43+
dfs(email, visited, connected_emails)
44+
merged_accounts.append([email_to_name[email]] + sorted(connected_emails))
45+
46+
return merged_accounts

0 commit comments

Comments
 (0)