forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
_721.java
153 lines (140 loc) · 6.46 KB
/
_721.java
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
/**
* 721. Accounts Merge
*
* 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.
* Now, we would like to merge these accounts.
* Two accounts definitely belong to the same person if there is some email that is common 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.
* 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.
Example 1:
Input:
accounts = [["John", "[email protected]", "[email protected]"], ["John", "[email protected]"], ["John", "[email protected]", "[email protected]"], ["Mary", "[email protected]"]]
Output: [["John", '[email protected]', '[email protected]', '[email protected]'], ["John", "[email protected]"], ["Mary", "[email protected]"]]
Explanation:
The first and third John's are the same person as they have the common email "[email protected]".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', '[email protected]'], ['John', '[email protected]'],
Note:
The length of accounts will be in the range [1, 1000].
The length of accounts[i] will be in the range [1, 10].
The length of accounts[i][j] will be in the range [1, 30].
*/
public class _721 {
public static class Solution1 {
/**credit: https://leetcode.com/articles/accounts-merge/#approach-1-depth-first-search-accepted
*
* Time Complexity: O(∑ai*logai) where ai is the length of accounts[i].
* 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.
* Space Complexity: O(∑ai) the space used by the graph and search.
* .*/
public List<List<String>> accountsMerge(List<List<String>> accounts) {
Map<String, String> emailToName = new HashMap();
Map<String, ArrayList<String>> graph = new HashMap();
for (List<String> account : accounts) {
String name = "";
for (String email : account) {
if (name == "") {
name = email;
continue;
}
graph.computeIfAbsent(email, x -> new ArrayList<>()).add(account.get(1));
graph.computeIfAbsent(account.get(1), x -> new ArrayList<>()).add(email);
emailToName.put(email, name);
}
}
Set<String> seen = new HashSet();
List<List<String>> ans = new ArrayList();
for (String email : graph.keySet()) {
if (!seen.contains(email)) {
seen.add(email);
Stack<String> stack = new Stack();
stack.push(email);
List<String> component = new ArrayList();
while (!stack.empty()) {
String node = stack.pop();
component.add(node);
for (String nei : graph.get(node)) {
if (!seen.contains(nei)) {
seen.add(nei);
stack.push(nei);
}
}
}
Collections.sort(component);
component.add(0, emailToName.get(email));
ans.add(component);
}
}
return ans;
}
}
public static class Solution2 {
/**credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted
* DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
*
* Time complexity: O(AlogA)
* Space complexity: O(A)
* */
public List<List<String>> accountsMerge(List<List<String>> accounts) {
DSU dsu = new DSU();
Map<String, String> emailToName = new HashMap<>();
Map<String, Integer> emailToId = new HashMap<>();
int id = 0;
for (List<String> account : accounts) {
String name = "";
for (String email : account) {
if (name.equals("")) {
name = email;
continue;
}
emailToName.put(email, name);
if (!emailToId.containsKey(email)) {
emailToId.put(email, id++);
}
dsu.union(emailToId.get(account.get(1)), emailToId.get(email));
}
}
Map<Integer, List<String>> ans = new HashMap<>();
for (String email : emailToName.keySet()) {
int index = dsu.find(emailToId.get(email));
ans.computeIfAbsent(index, x -> new ArrayList()).add(email);
}
for (List<String> component : ans.values()) {
Collections.sort(component);
component.add(0, emailToName.get(component.get(0)));
}
return new ArrayList<>(ans.values());
}
class DSU {
int[] parent;
public DSU() {
parent = new int[10001];
for (int i = 0; i <= 10000; i++) {
parent[i] = i;
}
}
public int find(int x) {
if (parent[x] != x) {
parent[x] = find(parent[x]);
}
return parent[x];
}
public void union(int x, int y) {
parent[find(x)] = find(y);
}
}
}
}