ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 1101. The Earliest Moment When Everyone Become Friends

2019-08-01 09:04:47  阅读:293  来源: 互联网

标签:Everyone logs int timestamp When Moment become friends log


原题链接在这里:https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/

题目:

In a social group, there are N people, with unique integer ids from 0 to N-1.

We have a list of logs, where each logs[i] = [timestamp, id_A, id_B] contains a non-negative integer timestamp, and the ids of two different people.

Each log represents the time in which two different people became friends.  Friendship is symmetric: if A is friends with B, then B is friends with A.

Let's say that person A is acquainted with 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. Return -1 if there is no such earliest time.

 

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 friend anything happens.
The sixth event occurs at timestamp = 20190301 and after 0 and 3 become friends we have that all become friends.

Note:

  1. 1 <= N <= 100
  2. 1 <= logs.length <= 10^4
  3. 0 <= logs[i][0] <= 10^9
  4. 0 <= logs[i][1], logs[i][2] <= N - 1
  5. It's guaranteed that all timestamps in logs[i][0] are different.
  6. Logs are not necessarily ordered by some criteria.
  7. logs[i][1] != logs[i][2]

题解:

If log[1] and log[2] do NOT have same ancestor, put them into same union. 

When count of unions become 1, that is the first time all people become friends and output time.

Time Complexity: O(mlogN). m = logs.length. find takes O(logN). With path compression and union by weight, amatorize O(1).

Space: O(N).

AC Java:

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

类似Friend Circles.

标签:Everyone,logs,int,timestamp,When,Moment,become,friends,log
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/11280351.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有