{x}
blog image

The Earliest Moment When Everyone Become Friends

Solution Explanation:

The problem asks to find the earliest time when everyone in a social group becomes acquainted with everyone else. Acquaintance is defined as being friends directly or indirectly (through a chain of friends). The input is an array of logs, where each log [timestamp, x, y] indicates that person x and person y become friends at the given timestamp.

The most efficient approach uses a combination of sorting and the Union-Find data structure.

1. Sorting:

The logs are first sorted in ascending order based on their timestamps. This ensures that we process friendships chronologically, allowing us to determine the earliest time everyone becomes acquainted.

2. Union-Find:

A Union-Find data structure is used to efficiently track connected components (groups of friends). Each person is initially in their own component. When two people become friends (a log entry is processed), their components are merged using the union operation of the Union-Find.

  • find(x): This function determines the root of the component containing person x. It uses path compression for optimization.
  • union(x, y): This function merges the components containing x and y. It often employs a heuristic (like union by rank or union by size) to improve efficiency, reducing the tree height in the Union-Find structure.

Algorithm:

  1. Sort Logs: Sort the logs array by timestamp.
  2. Initialize Union-Find: Create a UnionFind data structure with n elements, representing the n people.
  3. Iterate and Merge: Iterate through the sorted logs. For each log [timestamp, x, y]:
    • Check if x and y are already in the same component using find(x) and find(y).
    • If they are not in the same component, merge their components using union(x, y).
    • Decrement n (the number of components) after each successful merge.
    • If n becomes 1 (meaning everyone is in the same component), return the current timestamp.
  4. No Solution: If the loop completes without n becoming 1, it means everyone never became acquainted, so return -1.

Time Complexity:

  • Sorting the logs takes O(m log m) time, where m is the number of logs.
  • The Union-Find operations (find and union) have an amortized time complexity of almost O(1) due to path compression and union by rank/size heuristics. Therefore, iterating through the sorted logs and performing Union-Find operations takes O(m) time.
  • The overall time complexity is dominated by sorting: O(m log m).

Space Complexity:

  • The Union-Find data structure requires O(n) space, where n is the number of people.
  • Sorting may require extra space depending on the implementation, but typically it's O(log m) in-place sorting or O(m) space for merge sort.
  • The overall space complexity is O(n) or O(m), depending on the sorting implementation.

The provided code solutions in various languages demonstrate this algorithm efficiently. They implement the UnionFind structure and apply the steps described above. Each solution's time and space complexity align with the analysis.