The Earliest Moment When Everyone Become Friends in C++


Suppose in a social group, there are N different people, with unique integer ids from 0 to N-1. Here we have a list of logs, where each logs[i] = [time, id_A, id_B] contains a non-negative integer timestamp, and the ids of two different people. Each log is showing the time in which two different people became friends. 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. We have to find the earliest time for which every person became acquainted with every other person. If no such time has found, then return -1 So if the input is like: [[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 will be 20190301. This is because the first event occurs at timestamp = 20190101 and after the person 0 and 1 become friends we have the friendship groups [0,1], [2], [3], [4], [5]. Then the second event occurs at timestamp = 20190104 and after person 3 and 4 become friends we have the following friendship groups [0,1], [2], [3,4], [5]. After that the third event occurs at timestamp = 20190107 and after person 2 and 3 become friends we have the following friendship groups [0,1], [2,3,4], [5]. Then the fourth event occurs at timestamp = 20190211 and after person 1 and 5 become friends we have the following friendship groups [0,1,5], [2,3,4]. There after the fifth event occurs at timestamp = 20190224 and as the person 2 and 4 are already friend anything happens. Finally, the sixth event occurs at timestamp = 20190301 and after person 0 and 3 become friends we have that all become friends.

To solve this, we will follow these steps −

  • Define a method called find(), this will take a value x, this will work as follows −

  • if parents[x] is -1, then return x

  • parents[x] := find(parents[x])

  • return parents[x]

  • In the main method, it will work as follows −

  • Define two arrays called parents and rank, of size N, fill parents with -1, and fill rank with 1s

  • sort the logs

  • for each element i in logs −

    • perform union on i[1] and i[2]

    • find(i[2]) and find(i[1])

    • if N is in the rank array, then return i[0]

  • return -1

Example(C++)

Let us see the following implementation to get a better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
   public:
   int earliestAcq(vector<vector<int>>& logs, int N) {
      vector<int> ds (N, -1);
      sort(begin(logs), end(logs));
      for(vector<int> &k : logs) {
         if(un(k[1], k[2], ds) == N) return k[0];
      }
      return -1;
   }
   int un(int u, int v, vector<int> & ds) {
      u = find(u, ds);
      v = find(v, ds);
      if(u != v) {
         ds[v] += ds[u];
         ds[u] = v;
      }
      return -ds[v];
   }
   int find(int u, vector<int> & ds) {
      return ds[u] < 0? u : ds[u] = find(ds[u], ds);
   }
};
main(){
   vector<vector<int>> v = {
      {20190101,0,1},{20190104,3,4},{20190107,2,3},{20190211,1,5},
      {20190224,2,4},{20190301,0,3},{20190312,1,2},{20190322,4,5}
   };
   Solution ob;
   cout <<ob.earliestAcq(v, 6);
}

Input

[[20190101,0,1],[20190104,3,4],[20190107,2,3],[20190211,1,5],
[20190224,2,4],[20190301,0,3],[20190312,1,2],[20190322,4,5]]
6

Output

20190301

Updated on: 30-Apr-2020

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements