并查集,排序数组,当集合只为一个的时候,返回当前的时间
public class test {
public int earliestAcq(int[][] logs, int N){
Arrays.sort(logs, Comparator.comparingInt(a -> a[0]));
//Arrays.sort(logs, (a, b) -> (a[0] - b[0]));
Friend f = new Friend(N);
for(int[] log : logs){
f.union(log[1],log[2]);
if(f.circles == 1){
return log[0];
}
}
return -1;
}
class Friend{
int n;
int circles;
int[] leader;
Friend(int N){
n = N;
circles = N;
leader = new int[n];
for(int i=0; i<leader.length; i++){
leader[i] = i;
}
}
int find(int A){
while(A != leader[A]){
A = leader[A];
}
return A;
}
void union(int A, int B){
int leaderA = find(A);
int leaderB = find(B);
if(leaderA != leaderB){
leader[leaderA] = leaderB;
circles--;
}
}
}
}