Skip to content

Commit 922b659

Browse files
committed
feat: add more problem
1 parent 3e18fdb commit 922b659

File tree

9 files changed

+478
-1
lines changed

9 files changed

+478
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"problem_name": "accounts_merge",
3+
"solution_class_name": "Solution",
4+
"problem_number": "721",
5+
"problem_title": "Accounts Merge",
6+
"difficulty": "Medium",
7+
"topics": "Array, Hash Table, String, Depth-First Search, Breadth-First Search, Union Find, Sorting",
8+
"tags": ["grind-75"],
9+
"readme_description": "Given a list of `accounts` where 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.\n\nNow, 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.\n\nAfter 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**.",
10+
"readme_examples": [
11+
{
12+
"content": "```\nInput: accounts = [[\"John\",\"[email protected]\",\"[email protected]\"],[\"John\",\"[email protected]\",\"[email protected]\"],[\"Mary\",\"[email protected]\"],[\"John\",\"[email protected]\"]]\nOutput: [[\"John\",\"[email protected]\",\"[email protected]\",\"[email protected]\"],[\"Mary\",\"[email protected]\"],[\"John\",\"[email protected]\"]]\n```\n**Explanation:** The first and second John's are the same person as they have the common email \"[email protected]\". The third John and Mary are different people as none of their email addresses are used by other accounts."
13+
},
14+
{
15+
16+
}
17+
],
18+
"readme_constraints": "- `1 <= accounts.length <= 1000`\n- `2 <= accounts[i].length <= 10`\n- `1 <= accounts[i][j].length <= 30`\n- `accounts[i][0]` consists of English letters.\n- `accounts[i][j] (for j > 0)` is a valid email.",
19+
"readme_additional": "",
20+
"solution_imports": "",
21+
"solution_methods": [
22+
{
23+
"name": "accounts_merge",
24+
"parameters": "accounts: list[list[str]]",
25+
"return_type": "list[list[str]]",
26+
"dummy_return": "[]"
27+
}
28+
],
29+
"test_imports": "import pytest\nfrom leetcode_py.test_utils import logged_test\nfrom .solution import Solution",
30+
"test_class_name": "AccountsMerge",
31+
"test_helper_methods": [
32+
{ "name": "setup_method", "parameters": "", "body": "self.solution = Solution()" }
33+
],
34+
"test_methods": [
35+
{
36+
"name": "test_accounts_merge",
37+
"parametrize": "accounts, expected",
38+
"parametrize_typed": "accounts: list[list[str]], expected: list[list[str]]",
39+
40+
"body": "result = self.solution.accounts_merge(accounts)\n# Sort both result and expected for comparison since order doesn't matter\nresult_sorted = [sorted(account) for account in sorted(result)]\nexpected_sorted = [sorted(account) for account in sorted(expected)]\nassert result_sorted == expected_sorted"
41+
}
42+
],
43+
"playground_imports": "from solution import Solution",
44+
"playground_test_case": "# Example test case\naccounts = [[\"John\", \"[email protected]\", \"[email protected]\"], [\"John\", \"[email protected]\", \"[email protected]\"], [\"Mary\", \"[email protected]\"], [\"John\", \"[email protected]\"]]\nexpected = [[\"John\", \"[email protected]\", \"[email protected]\", \"[email protected]\"], [\"Mary\", \"[email protected]\"], [\"John\", \"[email protected]\"]]",
45+
"playground_execution": "result = Solution().accounts_merge(accounts)\nresult",
46+
"playground_assertion": "# Sort for comparison since order doesn't matter\nresult_sorted = [sorted(account) for account in sorted(result)]\nexpected_sorted = [sorted(account) for account in sorted(expected)]\nassert result_sorted == expected_sorted"
47+
}

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
PYTHON_VERSION = 3.13
2-
PROBLEM ?= k_closest_points_to_origin
2+
PROBLEM ?= accounts_merge
33
FORCE ?= 0
44

55
sync_submodules:

leetcode/accounts_merge/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Accounts Merge
2+
3+
**Difficulty:** Medium
4+
**Topics:** Array, Hash Table, String, Depth-First Search, Breadth-First Search, Union Find, Sorting
5+
**Tags:** grind-75
6+
7+
**LeetCode:** [Problem 721](https://leetcode.com/problems/accounts-merge/description/)
8+
9+
## Problem Description
10+
11+
Given a list of `accounts` where 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.
12+
13+
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.
14+
15+
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**.
16+
17+
## Examples
18+
19+
### Example 1:
20+
21+
```
22+
Input: accounts = [["John","[email protected]","[email protected]"],["John","[email protected]","[email protected]"],["Mary","[email protected]"],["John","[email protected]"]]
23+
24+
```
25+
26+
**Explanation:** The first and second John's are the same person as they have the common email "[email protected]". The third John and Mary are different people as none of their email addresses are used by other accounts.
27+
28+
### Example 2:
29+
30+
```
31+
32+
33+
```
34+
35+
## Constraints
36+
37+
- `1 <= accounts.length <= 1000`
38+
- `2 <= accounts[i].length <= 10`
39+
- `1 <= accounts[i][j].length <= 30`
40+
- `accounts[i][0]` consists of English letters.
41+
- `accounts[i][j] (for j > 0)` is a valid email.

leetcode/accounts_merge/__init__.py

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "imports",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"from solution import Solution"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"id": "setup",
17+
"metadata": {},
18+
"outputs": [],
19+
"source": [
20+
"# Example test case\n",
21+
"accounts = [\n",
22+
" [\"John\", \"[email protected]\", \"[email protected]\"],\n",
23+
" [\"John\", \"[email protected]\", \"[email protected]\"],\n",
24+
" [\"Mary\", \"[email protected]\"],\n",
25+
" [\"John\", \"[email protected]\"],\n",
26+
"]\n",
27+
"expected = [\n",
28+
" [\"John\", \"[email protected]\", \"[email protected]\", \"[email protected]\"],\n",
29+
" [\"Mary\", \"[email protected]\"],\n",
30+
" [\"John\", \"[email protected]\"],\n",
31+
"]"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": 3,
37+
"id": "execute",
38+
"metadata": {},
39+
"outputs": [
40+
{
41+
"data": {
42+
"text/plain": [
43+
44+
" ['Mary', '[email protected]'],\n",
45+
" ['John', '[email protected]']]"
46+
]
47+
},
48+
"execution_count": 3,
49+
"metadata": {},
50+
"output_type": "execute_result"
51+
}
52+
],
53+
"source": [
54+
"result = Solution().accounts_merge(accounts)\n",
55+
"result"
56+
]
57+
},
58+
{
59+
"cell_type": "code",
60+
"execution_count": 4,
61+
"id": "test",
62+
"metadata": {},
63+
"outputs": [],
64+
"source": [
65+
"# Sort for comparison since order doesn't matter\n",
66+
"result_sorted = [sorted(account) for account in sorted(result)]\n",
67+
"expected_sorted = [sorted(account) for account in sorted(expected)]\n",
68+
"assert result_sorted == expected_sorted"
69+
]
70+
}
71+
],
72+
"metadata": {
73+
"kernelspec": {
74+
"display_name": "leetcode-py-py3.13",
75+
"language": "python",
76+
"name": "python3"
77+
},
78+
"language_info": {
79+
"codemirror_mode": {
80+
"name": "ipython",
81+
"version": 3
82+
},
83+
"file_extension": ".py",
84+
"mimetype": "text/x-python",
85+
"name": "python",
86+
"nbconvert_exporter": "python",
87+
"pygments_lexer": "ipython3",
88+
"version": "3.13.7"
89+
}
90+
},
91+
"nbformat": 4,
92+
"nbformat_minor": 5
93+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution:
2+
# Time: O(N * M) where N is accounts, M is max emails per account
3+
# Space: O(N * M)
4+
def accounts_merge(self, accounts: list[list[str]]) -> list[list[str]]:
5+
email_to_accounts: dict[str, list[int]] = {}
6+
7+
for i, account in enumerate(accounts):
8+
for email in account[1:]:
9+
if email not in email_to_accounts:
10+
email_to_accounts[email] = []
11+
email_to_accounts[email].append(i)
12+
13+
visited: set[int] = set()
14+
result = []
15+
16+
def dfs(account_idx: int, emails: set[str]) -> None:
17+
if account_idx in visited:
18+
return
19+
visited.add(account_idx)
20+
21+
for email in accounts[account_idx][1:]:
22+
emails.add(email)
23+
for neighbor_idx in email_to_accounts[email]:
24+
dfs(neighbor_idx, emails)
25+
26+
for i in range(len(accounts)):
27+
if i in visited:
28+
continue
29+
30+
emails: set[str] = set()
31+
dfs(i, emails)
32+
result.append([accounts[i][0]] + sorted(emails))
33+
34+
return result

leetcode/accounts_merge/tests.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import pytest
2+
3+
from leetcode_py.test_utils import logged_test
4+
5+
from .solution import Solution
6+
7+
8+
class TestAccountsMerge:
9+
def setup_method(self):
10+
self.solution = Solution()
11+
12+
@pytest.mark.parametrize(
13+
"accounts, expected",
14+
[
15+
(
16+
[
17+
18+
19+
["Mary", "[email protected]"],
20+
["John", "[email protected]"],
21+
],
22+
[
23+
24+
["Mary", "[email protected]"],
25+
["John", "[email protected]"],
26+
],
27+
),
28+
(
29+
[
30+
31+
32+
33+
34+
35+
],
36+
[
37+
38+
39+
40+
41+
42+
],
43+
),
44+
# Single account
45+
(
46+
[["Alice", "[email protected]"]],
47+
[["Alice", "[email protected]"]],
48+
),
49+
# No merging needed - all separate
50+
(
51+
[
52+
["Alice", "[email protected]"],
53+
["Bob", "[email protected]"],
54+
["Charlie", "[email protected]"],
55+
],
56+
[
57+
["Alice", "[email protected]"],
58+
["Bob", "[email protected]"],
59+
["Charlie", "[email protected]"],
60+
],
61+
),
62+
# Chain merging - A->B->C
63+
(
64+
[
65+
66+
67+
68+
],
69+
70+
),
71+
# Multiple emails per account with complex merging
72+
(
73+
[
74+
75+
76+
["Sarah", "[email protected]"],
77+
78+
],
79+
[
80+
[
81+
"David",
82+
83+
84+
85+
86+
87+
],
88+
["Sarah", "[email protected]"],
89+
],
90+
),
91+
# Same name different people
92+
(
93+
[
94+
["John", "[email protected]"],
95+
["John", "[email protected]"],
96+
["John", "[email protected]"],
97+
],
98+
[
99+
["John", "[email protected]"],
100+
["John", "[email protected]"],
101+
["John", "[email protected]"],
102+
],
103+
),
104+
],
105+
)
106+
@logged_test
107+
def test_accounts_merge(self, accounts: list[list[str]], expected: list[list[str]]):
108+
result = self.solution.accounts_merge(accounts)
109+
# Sort both result and expected for comparison since order doesn't matter
110+
result_sorted = [sorted(account) for account in sorted(result)]
111+
expected_sorted = [sorted(account) for account in sorted(expected)]
112+
assert result_sorted == expected_sorted

0 commit comments

Comments
 (0)