Skip to content

Commit 0faa5d1

Browse files
update 721
1 parent 132db74 commit 0faa5d1

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

Diff for: src/main/java/com/fishercoder/solutions/_721.java

+18-15
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,13 @@ public List<List<String>> accountsMerge(List<List<String>> accounts) {
6666
public static class Solution2 {
6767
/**
6868
* credit: https://leetcode.com/articles/accounts-merge/#approach-2-union-find-accepted
69-
* DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure
69+
* DSU stands for Disjoint Set Union: https://en.wikipedia.org/wiki/Disjoint-set_data_structure, a.k.a Union Find data structure.
7070
* <p>
71-
* Time complexity: O(AlogA)
72-
* Space complexity: O(A)
71+
* Time complexity: O(nlogn)
72+
* Space complexity: O(n)
7373
*/
7474
public List<List<String>> accountsMerge(List<List<String>> accounts) {
75-
DSU dsu = new DSU();
75+
UnionFind uf = new UnionFind();
7676
Map<String, String> emailToName = new HashMap<>();
7777
Map<String, Integer> emailToId = new HashMap<>();
7878
int id = 0;
@@ -87,28 +87,31 @@ public List<List<String>> accountsMerge(List<List<String>> accounts) {
8787
if (!emailToId.containsKey(email)) {
8888
emailToId.put(email, id++);
8989
}
90-
dsu.union(emailToId.get(account.get(1)), emailToId.get(email));
90+
uf.union(emailToId.get(account.get(1)), emailToId.get(email));
9191
}
9292
}
9393

94-
Map<Integer, List<String>> ans = new HashMap<>();
94+
Map<Integer, List<String>> map = new HashMap<>();
9595
for (String email : emailToName.keySet()) {
96-
int index = dsu.find(emailToId.get(email));
97-
ans.computeIfAbsent(index, x -> new ArrayList()).add(email);
96+
//find the index of this email first: use this email's ID to find its parent in the Union Find
97+
int index = uf.find(emailToId.get(email));
98+
map.computeIfAbsent(index, x -> new ArrayList()).add(email);
9899
}
99-
for (List<String> component : ans.values()) {
100+
for (List<String> component : map.values()) {
100101
Collections.sort(component);
102+
//this is to add name to the head of the list
101103
component.add(0, emailToName.get(component.get(0)));
102104
}
103-
return new ArrayList<>(ans.values());
105+
return new ArrayList<>(map.values());
104106
}
105107

106-
class DSU {
108+
class UnionFind {
107109
int[] parent;
110+
int size = 10001;
108111

109-
public DSU() {
110-
parent = new int[10001];
111-
for (int i = 0; i <= 10000; i++) {
112+
public UnionFind() {
113+
parent = new int[size];
114+
for (int i = 0; i < size; i++) {
112115
parent[i] = i;
113116
}
114117
}
@@ -121,7 +124,7 @@ public int find(int x) {
121124
}
122125

123126
public void union(int x, int y) {
124-
parent[find(x)] = find(y);
127+
parent[find(x)] = find(y);//can be written as parent[find(y)] = find(x); they are equivalent
125128
}
126129
}
127130
}

Diff for: src/test/java/com/fishercoder/_721Test.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._721;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

77
import java.util.ArrayList;
88
import java.util.Arrays;
@@ -14,8 +14,8 @@ public class _721Test {
1414
private static List<List<String>> accounts;
1515
private static List<List<String>> expected;
1616

17-
@BeforeClass
18-
public static void setup() {
17+
@BeforeEach
18+
public void setup() {
1919
solution1 = new _721.Solution1();
2020
solution2 = new _721.Solution2();
2121
}

0 commit comments

Comments
 (0)