[LeetCode] 1101. The Earliest Moment When Everyone Become Friends

There are n people in a social group labeled from 0 to n - 1. You are given an array logs where logs[i] = [timestampi, xi, yi] indicates that xi and yi will be friends at the time timestampi.

Friendship is symmetric. That means if a is friends with b, then b is friends with a. Also, person a is acquainted with a person b if a is friends with b, or a is a friend of someone acquainted with b.

Return the earliest time for which every person became acquainted with every other person. If there is no such earliest time, return -1.

Example 1:

Input: logs = [[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]], n = 6
Output: 20190301
Explanation: 
The first event occurs at timestamp = 20190101, and after 0 and 1 become friends, we have the following friendship groups [0,1], [2], [3], [4], [5].
The second event occurs at timestamp = 20190104, and after 3 and 4 become friends, we have the following friendship groups [0,1], [2], [3,4], [5].
The third event occurs at timestamp = 20190107, and after 2 and 3 become friends, we have the following friendship groups [0,1], [2,3,4], [5].
The fourth event occurs at timestamp = 20190211, and after 1 and 5 become friends, we have the following friendship groups [0,1,5], [2,3,4].
The fifth event occurs at timestamp = 20190224, and as 2 and 4 are already friends, nothing happens.
The sixth event occurs at timestamp = 20190301, and after 0 and 3 become friends, we all become friends.

Example 2:

Input: logs = [[0,2,0],[1,0,1],[3,0,3],[4,1,2],[7,3,1]], n = 4
Output: 3
Explanation: At timestamp = 3, all the persons (i.e., 0, 1, 2, and 3) become friends.

Constraints:

  • 2 <= n <= 100
  • 1 <= logs.length <= 104
  • logs[i].length == 3
  • 0 <= timestampi <= 109
  • 0 <= xi, yi <= n - 1
  • xi != yi
  • All the values timestampi are unique.
  • All the pairs (xi, yi) occur at most one time in the input.

彼此熟识的最早时间。

题目的中文翻译参见这里。题目给的 logs 是一些人两两认识的关系和他们成为朋友的时间戳,要我们求的是最早什么时候所有人都互相认识。

思路是并查集。这道题类似547题,一开始每个人都自成一组,每个人都只认识自己。之后我们遍历 logs(注意 logs 需要按照时间戳排序),根据 logs 里给出的信息我们把人放到一些连通分量里。每处理完一条 log,我们就检查连通分量的个数。当连通分量 == 1 的时候,就说明所有人都被合并在一组里了,返回此时的时间戳。

时间O(nlogn) - n个点,每个点找自己的父节点的复杂度 ≈ logn

空间O(n)

Java实现

 1 class Solution {
 2     public int earliestAcq(int[][] logs, int n) {
 3         Arrays.sort(logs, (a, b) -> (a[0] - b[0]));
 4         UF uf = new UF(n);
 5         for (int[] log : logs) {
 6             int time = log[0];
 7             int a = log[1];
 8             int b = log[2];
 9             uf.union(a, b);
10             if (uf.count == 1) {
11                 return time;
12             }
13         }
14         return -1;
15     }
16 }
17 
18 class UF {
19     int[] parent;
20     int count;
21     
22     public UF(int n) {
23         parent = new int[n];
24         for (int i = 0; i < n; i++) {
25             parent[i] = i;
26         }
27         count = n;
28     }
29     
30     public int find(int i) {
31         if (i == parent[i]) {
32             return i;
33         }
34         return parent[i] = find(parent[i]);
35     }
36     
37     public void union(int p, int q) {
38         int pRoot = find(p);
39         int qRoot = find(q);
40         if (pRoot != qRoot) {
41             parent[pRoot] = qRoot;
42             count--;
43         }
44     }
45 }

 

相关题目

547. Friend Circles

1101. The Earliest Moment When Everyone Become Friends

LeetCode 题目总结

posted @ 2023-01-25 07:00  CNoodle  阅读(121)  评论(0编辑  收藏  举报