Skip to content

Commit 65f5f54

Browse files
solve #1101: the earliest moment when everyone become friend in java
1 parent b19d3cb commit 65f5f54

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@
498498
| 1086 | 🔒 [High Five](https://leetcode.com/problems/high-five) | | |
499499
| 1089 | [Duplicate Zeroes](https://leetcode.com/problems/duplicate-zeros) | [![Java](assets/java.png)](src/DuplicateZeros.java) | |
500500
| 1099 | 🔒 [Two Sum Less Than K](https://leetcode.com/problems/two-sum-less-than-k) | | |
501+
| 1101 | [The Earliest Moment When Everyone Become Friend](https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends) | [![Java](assets/java.png)](src/TheEarliestMomentWhenEveryoneBecomeFriends.java) | |
501502
| 1103 | [Distribute Candies to People](https://leetcode.com/problems/distribute-candies-to-people) | [![Java](assets/java.png)](src/DistributeCandiesToPeople.java) | |
502503
| 1108 | [Defanging an IP Address](https://leetcode.com/problems/defanging-an-ip-address) | [![Java](assets/java.png)](src/DefangingAnIPAddress.java) | |
503504
| 1118 | 🔒 [Number of Days in a Month](https://leetcode.com/problems/number-of-days-in-a-month) | | |

Diff for: src/TheEarliestMomentWhenEveryoneBecomeFriends.java

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends
2+
// M = |logs|
3+
// T: O(N + M logM + M al(N)) al = inverse Ackermann function
4+
// S: O(N + logM)
5+
6+
import java.util.Arrays;
7+
import java.util.Comparator;
8+
9+
public class TheEarliestMomentWhenEveryoneBecomeFriends {
10+
private static final class DisjointSet {
11+
private final int[] root, rank;
12+
13+
public DisjointSet(int size) {
14+
root = new int[size];
15+
rank = new int[size];
16+
for (int i = 0 ; i < size ; i++) {
17+
root[i] = i;
18+
rank[i] = 1;
19+
}
20+
}
21+
22+
public int find(int num) {
23+
if (num == root[num]) {
24+
return num;
25+
}
26+
return root[num] = find(root[num]);
27+
}
28+
29+
public boolean areConnected(int x, int y) {
30+
return find(x) == find(y);
31+
}
32+
33+
public void union(int x, int y) {
34+
final int rootX = find(x), rootY = find(y);
35+
if (rootX == rootY) {
36+
return;
37+
}
38+
if (rank[rootX] > rank[rootY]) {
39+
root[rootY] = rootX;
40+
} else if (rank[rootX] < rank[rootY]) {
41+
root[rootX] = rootY;
42+
} else {
43+
root[rootY] = rootX;
44+
rank[rootX]++;
45+
}
46+
}
47+
}
48+
49+
public int earliestAcq(int[][] logs, int n) {
50+
final DisjointSet disjointSet = new DisjointSet(n);
51+
Arrays.sort(logs, Comparator.comparingInt(a -> a[0]));
52+
for (int[] log : logs) {
53+
if (!disjointSet.areConnected(log[1], log[2])) {
54+
disjointSet.union(log[1], log[2]);
55+
n--;
56+
if (n == 1) {
57+
return log[0];
58+
}
59+
}
60+
}
61+
return -1;
62+
}
63+
}

0 commit comments

Comments
 (0)