|
9 | 9 | import java.util.Set;
|
10 | 10 | import java.util.Stack;
|
11 | 11 |
|
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 |
| - */ |
40 | 12 | public class _721 {
|
41 | 13 |
|
42 | 14 | 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> |
45 | 18 | * Time Complexity: O(∑ai*logai) where ai is the length of accounts[i].
|
46 | 19 | * 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.
|
47 | 20 | * Space Complexity: O(∑ai) the space used by the graph and search.
|
48 |
| - * .*/ |
| 21 | + * . |
| 22 | + */ |
49 | 23 | public List<List<String>> accountsMerge(List<List<String>> accounts) {
|
50 | 24 | Map<String, String> emailToName = new HashMap();
|
51 | 25 | Map<String, ArrayList<String>> graph = new HashMap();
|
@@ -90,12 +64,13 @@ public List<List<String>> accountsMerge(List<List<String>> accounts) {
|
90 | 64 | }
|
91 | 65 |
|
92 | 66 | 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 |
94 | 69 | * DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
|
95 |
| - * |
| 70 | + * <p> |
96 | 71 | * Time complexity: O(AlogA)
|
97 | 72 | * Space complexity: O(A)
|
98 |
| - * */ |
| 73 | + */ |
99 | 74 | public List<List<String>> accountsMerge(List<List<String>> accounts) {
|
100 | 75 | DSU dsu = new DSU();
|
101 | 76 | Map<String, String> emailToName = new HashMap<>();
|
|
0 commit comments