Skip to content

Commit 996a9e5

Browse files
refactor 721
1 parent 2e70c28 commit 996a9e5

File tree

1 file changed

+9
-34
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+9
-34
lines changed

src/main/java/com/fishercoder/solutions/_721.java

Lines changed: 9 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,43 +9,17 @@
99
import java.util.Set;
1010
import java.util.Stack;
1111

12-
/**
13-
* 721. Accounts Merge
14-
*
15-
* 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.
16-
* Now, we would like to merge these accounts.
17-
* Two accounts definitely belong to the same person if there is some email that is common to both accounts.
18-
* Note that even if two accounts have the same name, they may belong to different people as people could have the same name.
19-
* A person can have any number of accounts initially, but all of their accounts definitely have the same name.
20-
* After merging the accounts, return the accounts in the following format:
21-
* the first element of each account is the name, and the rest of the elements are emails in sorted order.
22-
* The accounts themselves can be returned in any order.
23-
24-
Example 1:
25-
Input:
26-
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
27-
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'], ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
28-
29-
Explanation:
30-
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
31-
The second John and Mary are different people as none of their email addresses are used by other accounts.
32-
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'],
33-
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
34-
35-
Note:
36-
The length of accounts will be in the range [1, 1000].
37-
The length of accounts[i] will be in the range [1, 10].
38-
The length of accounts[i][j] will be in the range [1, 30].
39-
*/
4012
public class _721 {
4113

4214
public static class Solution1 {
43-
/**credit: https://leetcode.com/articles/accounts-merge/#approach-1-depth-first-search-accepted
44-
*
15+
/**
16+
* credit: https://leetcode.com/articles/accounts-merge/#approach-1-depth-first-search-accepted
17+
* <p>
4518
* Time Complexity: O(∑ai*logai) where a​i is the length of accounts[i].
4619
* Without the log factor, this is the complexity to build the graph and search for each component. The log factor is for sorting each component at the end.
4720
* Space Complexity: O(∑ai) the space used by the graph and search.
48-
* .*/
21+
* .
22+
*/
4923
public List<List<String>> accountsMerge(List<List<String>> accounts) {
5024
Map<String, String> emailToName = new HashMap();
5125
Map<String, ArrayList<String>> graph = new HashMap();
@@ -90,12 +64,13 @@ public List<List<String>> accountsMerge(List<List<String>> accounts) {
9064
}
9165

9266
public static class Solution2 {
93-
/**credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted
67+
/**
68+
* credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted
9469
* DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
95-
*
70+
* <p>
9671
* Time complexity: O(AlogA)
9772
* Space complexity: O(A)
98-
* */
73+
*/
9974
public List<List<String>> accountsMerge(List<List<String>> accounts) {
10075
DSU dsu = new DSU();
10176
Map<String, String> emailToName = new HashMap<>();

0 commit comments

Comments
 (0)