The Earliest Moment When Everyone Become Friends Problem
Description
LeetCode Problem 1101.
There are n people in a social group labeled from 0 to n - 1. You are given an array logs where logs[i] = [timestamp_i, x_i, y_i] indicates that x_i and y_i will be friends at the time timestamp_i.
Friendship is symmetric. That means if a is friends with b, then b is friends with a. Also, person a is acquainted with a 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. If there is no such earliest time, return -1.
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 friends, nothing happens.
The sixth event occurs at timestamp = 20190301, and after 0 and 3 become friends, we all become friends.
Example 2:
Input: logs = [[0,2,0],[1,0,1],[3,0,3],[4,1,2],[7,3,1]], n = 4
Output: 3
Explanation: At timestamp = 3, all the persons (i.e., 0, 1, 2, and 3) become friends.
Constraints:
2 <= n <= 1001 <= logs.length <= 10^4logs[i].length == 30 <= timestamp_i <= 10^90 <= x_i, y_i <= n - 1x_i != y_i- All the values
timestamp_iare unique. - All the pairs
(x_i, y_i)occur at most one time in the input.
Solution
Approach: Sort by Time + Union-Find
"Acquainted" is just "connected" in a graph where people are nodes and friendships are edges. Everyone knows everyone exactly when the graph has a single connected component. Friendships are never removed, so once that happens it stays true, and we want the first moment it happens.
The logs are not given in order, so sort them by timestamp first and add the friendships one by one. A union-find (disjoint set) structure keeps track of the groups:
- Start with
ngroups, one person each. - For a log
[time, x, y], find the group roots ofxandy. If they are the same,xandyalready knew each other through others, and nothing changes. - Otherwise merge the two groups and decrease the group count. When it reaches
1, the currenttimeis the answer.
If the logs run out while there is still more than one group, someone never got connected: return -1.
Complexity: O(m log m) time for sorting the m logs (the union-find steps are almost O(1) each), and O(n) extra space for the parent array.
class Solution:
def earliestAcq(self, logs: List[List[int]], n: int) -> int:
parent = list(range(n))
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
groups = n
for time, x, y in sorted(logs): # oldest friendship first
rx, ry = find(x), find(y)
if rx == ry:
continue # already acquainted
parent[rx] = ry
groups -= 1
if groups == 1:
return time
return -1