Skip to content

Commit f527811

Browse files
[N-0] add 721
1 parent 040142d commit f527811

File tree

3 files changed

+260
-0
lines changed

3 files changed

+260
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|721|[Accounts Merge](https://leetcode.com/problems/accounts-merge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_721.java) | | | Medium | DFS, Union Find
2526
|720|[Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_720.java) | O(∑wi) where wi is the length of words[i] | O(∑wi) where wi is the length of words[i] | Easy | Trie
2627
|719|[Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_719.java) | O(nlogw + nlogn) | O(1) | Hard | Binary Search
2728
|718|[Maximum Length of Repeated Subarray](https://leetcode.com/problems/maximum-length-of-repeated-subarray/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_718.java) | O(m*n) | O(m*n) | Medium | DP
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Set;
10+
import java.util.Stack;
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+
public class _721 {
41+
42+
public static class Solution1 {
43+
/**credit: https://leetcode.com/articles/accounts-merge/#approach-1-depth-first-search-accepted
44+
*
45+
* Time Complexity: O(∑ai*logai) where a​i is the length of accounts[i].
46+
* 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+
* Space Complexity: O(∑ai) the space used by the graph and search.
48+
* .*/
49+
public List<List<String>> accountsMerge(List<List<String>> accounts) {
50+
Map<String, String> emailToName = new HashMap();
51+
Map<String, ArrayList<String>> graph = new HashMap();
52+
for (List<String> account : accounts) {
53+
String name = "";
54+
for (String email : account) {
55+
if (name == "") {
56+
name = email;
57+
continue;
58+
}
59+
graph.computeIfAbsent(email, x -> new ArrayList<>()).add(account.get(1));
60+
graph.computeIfAbsent(account.get(1), x -> new ArrayList<>()).add(email);
61+
emailToName.put(email, name);
62+
}
63+
}
64+
65+
Set<String> seen = new HashSet();
66+
List<List<String>> ans = new ArrayList();
67+
for (String email : graph.keySet()) {
68+
if (!seen.contains(email)) {
69+
seen.add(email);
70+
Stack<String> stack = new Stack();
71+
stack.push(email);
72+
List<String> component = new ArrayList();
73+
while (!stack.empty()) {
74+
String node = stack.pop();
75+
component.add(node);
76+
for (String nei : graph.get(node)) {
77+
if (!seen.contains(nei)) {
78+
seen.add(nei);
79+
stack.push(nei);
80+
}
81+
}
82+
}
83+
Collections.sort(component);
84+
component.add(0, emailToName.get(email));
85+
ans.add(component);
86+
}
87+
}
88+
return ans;
89+
}
90+
}
91+
92+
public static class Solution2 {
93+
/**credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted
94+
* DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
95+
*
96+
* Time complexity: O(AlogA)
97+
* Space complexity: O(A)
98+
* */
99+
public List<List<String>> accountsMerge(List<List<String>> accounts) {
100+
DSU dsu = new DSU();
101+
Map<String, String> emailToName = new HashMap<>();
102+
Map<String, Integer> emailToId = new HashMap<>();
103+
int id = 0;
104+
for (List<String> account : accounts) {
105+
String name = "";
106+
for (String email : account) {
107+
if (name.equals("")) {
108+
name = email;
109+
continue;
110+
}
111+
emailToName.put(email, name);
112+
if (!emailToId.containsKey(email)) {
113+
emailToId.put(email, id++);
114+
}
115+
dsu.union(emailToId.get(account.get(1)), emailToId.get(email));
116+
}
117+
}
118+
119+
Map<Integer, List<String>> ans = new HashMap<>();
120+
for (String email : emailToName.keySet()) {
121+
int index = dsu.find(emailToId.get(email));
122+
ans.computeIfAbsent(index, x -> new ArrayList()).add(email);
123+
}
124+
for (List<String> component : ans.values()) {
125+
Collections.sort(component);
126+
component.add(0, emailToName.get(component.get(0)));
127+
}
128+
return new ArrayList<>(ans.values());
129+
}
130+
131+
class DSU {
132+
int[] parent;
133+
134+
public DSU() {
135+
parent = new int[10001];
136+
for (int i = 0; i <= 10000; i++) {
137+
parent[i] = i;
138+
}
139+
}
140+
141+
public int find(int x) {
142+
if (parent[x] != x) {
143+
parent[x] = find(parent[x]);
144+
}
145+
return parent[x];
146+
}
147+
148+
public void union(int x, int y) {
149+
parent[find(x)] = find(y);
150+
}
151+
}
152+
}
153+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._721;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
11+
public class _721Test {
12+
private static _721.Solution1 solution1;
13+
private static _721.Solution2 solution2;
14+
private static List<List<String>> accounts;
15+
private static List<List<String>> expected;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _721.Solution1();
20+
solution2 = new _721.Solution2();
21+
}
22+
23+
@Test
24+
public void test1() throws Exception {
25+
accounts = new ArrayList<>();
26+
List<String> account1 = new ArrayList<>(Arrays.asList("John", "johnsmith@mail.com", "john00@mail.com"));
27+
List<String> account2 = new ArrayList<>(Arrays.asList("John", "johnnybravo@mail.com"));
28+
List<String> account3 = new ArrayList<>(Arrays.asList("John", "johnsmith@mail.com", "john_newyork@mail.com"));
29+
List<String> account4 = new ArrayList<>(Arrays.asList("Mary", "mary@mail.com"));
30+
accounts.add(account1);
31+
accounts.add(account2);
32+
accounts.add(account3);
33+
accounts.add(account4);
34+
35+
expected = new ArrayList<>();
36+
List<String> expected1 = new ArrayList<>(Arrays.asList("Mary", "mary@mail.com"));
37+
List<String> expected2 = new ArrayList<>(Arrays.asList("John", "john00@mail.com", "john_newyork@mail.com", "johnsmith@mail.com"));
38+
List<String> expected3 = new ArrayList<>(Arrays.asList("John", "johnnybravo@mail.com"));
39+
expected.add(expected1);
40+
expected.add(expected2);
41+
expected.add(expected3);
42+
43+
assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts));
44+
assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts));
45+
}
46+
47+
private void assertEqualsIgnoreOrdering(List<List<String>> expected, List<List<String>> actual) throws Exception {
48+
//TODO: implement this method
49+
if (true) {
50+
return;
51+
} else {
52+
throw new Exception();
53+
}
54+
}
55+
56+
@Test
57+
public void test2() throws Exception {
58+
accounts = new ArrayList<>();
59+
List<String> account1 = new ArrayList<>(Arrays.asList("Alex", "Alex5@m.co", "Alex4@m.co", "Alex0@m.co"));
60+
List<String> account2 = new ArrayList<>(Arrays.asList("Ethan", "Ethan3@m.co", "Ethan3@m.co", "Ethan0@m.co"));
61+
List<String> account3 = new ArrayList<>(Arrays.asList("Kevin", "Kevin4@m.co", "Kevin2@m.co", "Kevin2@m.co"));
62+
List<String> account4 = new ArrayList<>(Arrays.asList("Gabe", "Gabe0@m.co", "Gabe3@m.co", "Gabe2@m.co"));
63+
List<String> account5 = new ArrayList<>(Arrays.asList("Gabe", "Gabe3@m.co", "Gabe4@m.co", "Gabe2@m.co"));
64+
accounts.add(account1);
65+
accounts.add(account2);
66+
accounts.add(account3);
67+
accounts.add(account4);
68+
accounts.add(account5);
69+
70+
expected = new ArrayList<>();
71+
List<String> expected1 = new ArrayList<>(Arrays.asList("Alex", "Alex0@m.co", "Alex4@m.co", "Alex5@m.co"));
72+
List<String> expected2 = new ArrayList<>(Arrays.asList("Kevin", "Kevin2@m.co", "Kevin4@m.co"));
73+
List<String> expected3 = new ArrayList<>(Arrays.asList("Ethan", "Ethan0@m.co", "Ethan3@m.co"));
74+
List<String> expected4 = new ArrayList<>(Arrays.asList("Gabe", "Gabe0@m.co", "Gabe2@m.co", "Gabe3@m.co", "Gabe4@m.co"));
75+
expected.add(expected1);
76+
expected.add(expected2);
77+
expected.add(expected3);
78+
expected.add(expected4);
79+
80+
assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts));
81+
assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts));
82+
}
83+
84+
@Test
85+
public void test3() throws Exception {
86+
accounts = new ArrayList<>();
87+
List<String> account1 = new ArrayList<>(Arrays.asList("David", "David0@m.co", "David1@m.co"));
88+
List<String> account2 = new ArrayList<>(Arrays.asList("David", "David3@m.co", "David4@m.co"));
89+
List<String> account3 = new ArrayList<>(Arrays.asList("David", "David4@m.co", "David5@m.co"));
90+
List<String> account4 = new ArrayList<>(Arrays.asList("David", "David2@m.co", "David3@m.co"));
91+
List<String> account5 = new ArrayList<>(Arrays.asList("David", "David1@m.co", "David2@m.co"));
92+
accounts.add(account1);
93+
accounts.add(account2);
94+
accounts.add(account3);
95+
accounts.add(account4);
96+
accounts.add(account5);
97+
98+
expected = new ArrayList<>();
99+
List<String> expected1 = new ArrayList<>(Arrays.asList("David", "David0@m.co", "David1@m.co", "David2@m.co", "David3@m.co", "David4@m.co", "David5@m.co"));
100+
expected.add(expected1);
101+
102+
assertEqualsIgnoreOrdering(expected, solution1.accountsMerge(accounts));
103+
assertEqualsIgnoreOrdering(expected, solution2.accountsMerge(accounts));
104+
}
105+
106+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy