原题链接在这里: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:

 class Solution {
public int earliestAcq(int[][] logs, int N) {
UF uf= new UF(N);
Arrays.sort(logs, (a, b)-> a[0]-b[0]); for(int [] log : logs){
if(uf.find(log[1]) != uf.find(log[2])){
uf.union(log[1], log[2]);
} if(uf.count == 1){
return log[0];
}
} return -1;
}
} class UF{
int [] parent;
int [] size;
int count; public UF(int n){
this.parent = new int[n];
this.size = new int[n];
for(int i = 0; i<n; i++){
parent[i] = i;
size[i] = 1;
} this.count = n;
} public int find(int i){
while(i != parent[i]){
parent[i] = parent[parent[i]];
i = parent[i];
} return parent[i];
} public void union(int p, int q){
int i = find(p);
int j = find(q);
if(size[i] > size[j]){
parent[j] = i;
size[i] += size[j];
}else{
parent[i] = j;
size[j] += size[i];
} this.count--;
}
}

类似Friend Circles.

LeetCode 1101. The Earliest Moment When Everyone Become Friends的更多相关文章

  1. 【LeetCode】1101. The Earliest Moment When Everyone Become Friends 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  2. 【LeetCode】1102. Path With Maximum Minimum Value 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序+并查集 优先级队列 日期 题目地址:https: ...

  3. 【LeetCode】323. Number of Connected Components in an Undirected Graph 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 并查集 日期 题目地址:https://leetcod ...

  4. [LeetCode] Design Hit Counter 设计点击计数器

    Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...

  5. [LeetCode] Generalized Abbreviation 通用简写

    Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...

  6. [LeetCode] Bitwise AND of Numbers Range 数字范围位相与

    Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers ...

  7. LeetCode Counting Bits

    原题链接在这里:https://leetcode.com/problems/counting-bits/ 题目: Given a non negative integer number num. Fo ...

  8. LeetCode Design Hit Counter

    原题链接在这里:https://leetcode.com/problems/design-hit-counter/. 题目: Design a hit counter which counts the ...

  9. [LeetCode] Prime Number of Set Bits in Binary Representation 二进制表示中的非零位个数为质数

    Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a prime ...

随机推荐

  1. Delphi TButton.OnClick 匿名函数用法

    type TNotifyEventRef = reference to procedure(Sender: TObject); function AnonymousEvent(const Proc: ...

  2. Python之路【第十六篇】:Python并发编程|进程、线程

    一.进程和线程 进程 假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输入(I/O操作), 而此时CPU只能静静地等待任务A读取完数据才能继续执行,这样就白白浪费了CPU资源. 是 ...

  3. 客开监控(BE/UI/BP)插件停用与启用

    1.单据界面右键属性,获取当前客开监控页面URL连接:http://172.16.168.15/U9/erp/display.aspx?lnk=UFSoft.UBF.Cust.CustManager& ...

  4. VMware Workstation 15 Player使用centos页面版本如何查看ip

    首先运行要使用的centos镜像,输入密码登陆进去 因为是界面版,所以就不需要再镜像中输入命令,但是因为这样又找不到没法用ifconfig查看ip怎么办?  这个就是类似于一个系统页面版本的linux ...

  5. adminLTE2.4.18 bootstrap3 左侧菜单高亮

    adminLTE2.4.18  bootstrap3  左侧菜单高亮 //菜单 $(function(){ console.log('eeeeeeeeee'); $('.sidebar-menu li ...

  6. python代码执行SQL文件(逐句执行)

    一.简介 关于Python如何连接数据库并执行SQL语句,几乎所有的Python教程都会讲,教程里基本只介绍了执行单条SQL语句的方法,但是实际生产过程中可不只是执行一两条语句,动辄几十条甚至上百条的 ...

  7. 关于zynq系列板卡设计VREFP_0参考电压的疑问及解答

    使用板卡:Z-turn Board 芯片:Xilinx Zynq-7010/7020处理器 有工程师在试用zynq系列Z-turn Board时提出:在原理图P3页 Bank0上VREFP_0端接地的 ...

  8. pandas-03 DataFrame()中的iloc和loc用法

    pandas-03 DataFrame()中的iloc和loc用法 简单的说: iloc,即index locate 用index索引进行定位,所以参数是整型,如:df.iloc[10:20, 3:5 ...

  9. vue从零开始(三)指令

    v-bind的使用 <!-- 绑定一个属性 --> <img v-bind:src="imageSrc"> <!-- 动态特性名 (2.6.0+) - ...

  10. EVM靶机渗透

    前言 靶机下载地址: https://download.vulnhub.com/norzhctf/Basilic.ova 通过渗透靶机来学习相关知识以便提高在真实环境的渗透能力 ==. 安装: 我是用 ...