LeetCode 1101. The Earliest Moment When Everyone Become Friends
原题链接在这里: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 <= N <= 100
1 <= logs.length <= 10^4
0 <= logs[i][0] <= 10^9
0 <= logs[i][1], logs[i][2] <= N - 1
- It's guaranteed that all timestamps in logs[i][0] are different.
Logs
are not necessarily ordered by some criteria.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.
转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11280351.html
LeetCode 1101. The Earliest Moment When Everyone Become Friends相关推荐
- LeetCode 1101. 彼此熟识的最早时间(排序+并查集)
文章目录 1. 题目 2. 解题 1. 题目 在一个社交圈子当中,有 N 个人.每个人都有一个从 0 到 N-1 唯一的 id 编号. 我们有一份日志列表 logs,其中每条记录都包含一个非负整数的时 ...
- LeetCode 1697. 检查边长度限制的路径是否存在(排序+并查集)
文章目录 1. 题目 2. 解题 1. 题目 给你一个 n 个点组成的无向图边集 edgeList ,其中 edgeList[i] = [ui, vi, disi] 表示点 ui 和点 vi 之间有一 ...
- LeetCode简单题之二进制表示中质数个计算置位
题目 给你两个整数 left 和 right ,在闭区间 [left, right] 范围内,统计并返回 计算置位位数为质数 的整数个数. 计算置位位数 就是二进制表示中 1 的个数. 例如, 21 ...
- 力扣(LeetCode)刷题,简单题(第16期)
目录 第1题:数组异或操作 第2题:交换数字 第3题:按既定顺序创建目标数组 第4题:数组中两元素的最大乘积 第5题:删除链表中的节点 第6题:在既定时间做作业的学生人数 第7题:二进制链表转整数 第 ...
- LeetCode 1869. 哪种连续子字符串更长
文章目录 1. 题目 2. 解题 1. 题目 给你一个二进制字符串 s .如果字符串中由 1 组成的 最长 连续子字符串 严格长于 由 0 组成的 最长 连续子字符串,返回 true :否则,返回 f ...
- LeetCode 第 186 场周赛(1060/3107,前34.1%)
文章目录 1. 比赛结果 2. 题目 1. LeetCode 5392. 分割字符串的最大得分 easy 2. LeetCode 5393. 可获得的最大点数 medium 3. LeetCode 5 ...
- LeetCode 1404. 将二进制表示减到 1 的步骤数(字符串加法)
1. 题目 给你一个以二进制形式表示的数字 s .请你返回按下述规则将其减少到 1 所需要的步骤数: 如果当前数字为偶数,则将其除以 2 . 如果当前数字为奇数,则将其加上 1 . 题目保证你总是可以 ...
- LeetCode 260. 只出现一次的数字 III(位运算)
1. 题目 给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次. 找出只出现一次的那两个元素. 示例 :输入: [1,2,1,3,2,5] 输出: [3,5] 注意: 结 ...
- c语言a b的和 不用加号,力扣(LeetCode)刷题,简单题(第16期)
目录 力扣(LeetCode)定期刷题,每期10道题,业务繁重的同志可以看看我分享的思路,不是最高效解决方案,只求互相提升. 第1题:数组异或操作 试题要求如下: 回答(C语言): int xorOp ...
- LeetCode——89.格雷编码
一.题目 n 位格雷码序列 是一个由 2n 个整数组成的序列,其中: 每个整数都在范围 [0, 2n - 1] 内(含 0 和 2n - 1) 第一个整数是 0 一个整数在序列中出现 不超过一次 每对 ...
最新文章
- 单例模式 之 单例模式——枚举
- Windows与Linux共享文件夹互相访问
- 停用nfs导致cacti无法抓取snmp数据
- 兰州大学第一届 飞马杯 ★★快乐苹果树★★ 树链剖分 + 懒标记 + 树状数组
- java 海量文件存储_【直通BAT】海量数据面试总结
- c语言 poll,c语言 linux 中 poll 的参数
- Oracle统计产生日志数据增长增量
- @Scheduled(cron=) spring定时任务时间设置
- 我的课程表--项目需求分析
- IIS中间件渗透总结
- CSS实现空心三角指示箭头原理
- There is no getter for property named ‘pCode‘ in ‘classXXX‘
- 第7章 网站前台-吐槽与问答
- php 图片印章_PHP制作中文圆形印章示例
- 关于tink的碰撞检测类【1】
- 解决QQ安全进程(护盾)弹出问题
- mysql唯一key_MySQL唯一约束(UNIQUE KEY)
- comsol计算机模拟过程,基于COMSOL的甲苯催化燃烧过程的数值模拟
- 怎样才能使自己的经络运行顺畅呢?
- “我以项上猪头担保,小蹄儿抖三抖,春节祝福就上链了!”