题目地址:https://leetcode-cn.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:

  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
  2. Output: 20190301
  3. Explanation:
  4. 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].
  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].
  6. 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].
  7. 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].
  8. The fifth event occurs at timestamp = 20190224 and as 2 and 4 are already friend anything happens.
  9. The sixth event occurs at timestamp = 20190301 and after 0 and 3 become friends we have that all become friends.

Note:

  1. 2 <= 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]

题目大意

在一个社交圈子当中,有 N 个人。每个人都有一个从 0 到 N-1 唯一的 id 编号。
我们有一份日志列表 logs,其中每条记录都包含一个非负整数的时间戳,以及分属两个人的不同 id,logs[i] = [timestamp, id_A, id_B]。
每条日志标识出两个人成为好友的时间,友谊是相互的:如果 A 和 B 是好友,那么 B 和 A 也是好友。
如果 A 是 B 的好友,或者 A 是 B 的好友的好友,那么就可以认为 A 也与 B 熟识。
返回圈子里所有人之间都熟识的最早时间。如果找不到最早时间,就返回 -1 。

解题方法

并查集

提示的不能更明显了,标准的并查集。

  1. 对logs按照时间排序。
  2. 遍历logs,合并两个人所属的环,如果环减少到1那就是最短的时间。

C++代码如下:

  1. class Solution {
  2. public:
  3. int earliestAcq(vector<vector<int>>& logs, int N) {
  4. map_ = vector<int>(N);
  5. circle = N;
  6. for (int i = 0; i < N; ++i)
  7. map_[i] = i;
  8. sort(logs.begin(), logs.end(), [](vector<int>& a, vector<int>& b) {return a[0] < b[0];});
  9. for (auto& log : logs) {
  10. uni(log[1], log[2]);
  11. if (circle == 1)
  12. return log[0];
  13. }
  14. return -1;
  15. }
  16. int find(int a) {
  17. if (map_[a] == a)
  18. return a;
  19. return find(map_[a]);
  20. }
  21. void uni(int a, int b) {
  22. int pa = find(a);
  23. int pb = find(b);
  24. if (pa == pb)
  25. return;
  26. map_[pa] = pb;
  27. circle --;
  28. }
  29. private:
  30. vector<int> map_;
  31. int circle = 0;
  32. };

日期

2019 年 9 月 21 日 —— 莫生气,我若气病谁如意

【LeetCode】1101. The Earliest Moment When Everyone Become Friends 解题报告 (C++)的更多相关文章

  1. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  2. 【LeetCode】430. Flatten a Multilevel Doubly Linked List 解题报告(Python)

    [LeetCode]430. Flatten a Multilevel Doubly Linked List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  3. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  4. 【LeetCode】373. Find K Pairs with Smallest Sums 解题报告(Python)

    [LeetCode]373. Find K Pairs with Smallest Sums 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/p ...

  5. LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法

    Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...

  6. LeetCode 1101. The Earliest Moment When Everyone Become Friends

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

  7. LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告

    https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...

  8. LeetCode 852. Peak Index in a Mountain Array C++ 解题报告

    852. Peak Index in a Mountain Array -- Easy 方法一:二分查找 int peakIndexInMountainArray(vector<int>& ...

  9. LeetCode: Populating Next Right Pointers in Each Node II 解题报告

    Populating Next Right Pointers in Each Node IIFollow up for problem "Populating Next Right Poin ...

随机推荐

  1. selenium+chrome抓取数据,运行js

    某些特殊的网站需要用selenium来抓取数据,比如用js加密的,破解难度大的 selenium支持linux和win,前提是必须安装python3,环境配置好 抓取代码: #!/usr/bin/en ...

  2. 巩固javaweb第十三天

    巩固内容: HTML 表格 表格由 <table> 标签来定义.每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义).字母 ...

  3. 日常Java 2021/11/15

    Applet类 每一个Applet都是java.applet Applet类的子类,基础的Applet类提供了供衍生类调用的方法,以此来得到浏览器上下文的信息和服务.这些方法做了如下事情: 得到App ...

  4. 双向链表——Java实现

    双向链表 链表是是一种重要的数据结构,有单链表和双向链表之分:本文我将重点阐述不带头结点的双向链表: 不带头结点的带链表 我将对双链表的增加和删除元素操作进行如下解析 1.增加元素(采用尾插法) (1 ...

  5. Dubbo服务分组

    服务分组与多版本控制的使用方式几乎是相同的,只要将version替换为group即可.但使用目的不同.使用版本控制的目的是为了升级,将原有老版本替换掉,将来不再提供老版本的服务,所以不同版本间不能出现 ...

  6. 【Linux】【Services】【SaaS】Docker+kubernetes(10. 利用反向代理实现服务高可用)

    1. 简介 1.1. 由于K8S并没有自己的集群,所以需要借助其他软件来实现,公司的生产环境使用的是Nginx,想要支持TCP转发要额外安装模块,测试环境中我就使用HAPROXY了 1.2. 由于是做 ...

  7. 安装本地jar包到仓库

    1. 下载并解压 sdk 包本地文件夹下 2. 进入项目目录 执行以下操作之前,先确定 maven 的 settings 文件中 配置的 仓库地址是否为本项目的 仓库地址,如果不是,则会安装到其他仓库 ...

  8. 【Linux】【Shell】【text】grep

    grep: Global search REgular expression and Print out the line. 作用:文本搜索工具,根据用户指定的"模式(过滤条件)" ...

  9. Nginx中指令

    Rewrite模块 1 return指令 Syntax: return code [text]; return code URL; return URL; Default: - Context: se ...

  10. 微信浏览器打开H5页面右上角隐藏转发功能

    js设置转发开关 document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { WeixinJSBridge. ...