The Earliest Moment When Everyone Become Friends - Problem

You're managing a social network where friendships form over time. Given n people labeled from 0 to n-1, you need to find the earliest moment when everyone becomes connected in one giant social circle.

You're given an array logs where logs[i] = [timestamp, x, y] indicates that person x and person y become friends at time timestamp. Remember:

  • Friendship is symmetric: if A is friends with B, then B is friends with A
  • Friendship is transitive through acquaintances: if A knows B and B knows C, then A is acquainted with C

Goal: Return the earliest timestamp when every person is acquainted with every other person (directly or through mutual friends). If this never happens, return -1.

Example: With 4 people and friendships forming at different times, you need to find when the last "island" of people connects to form one unified group.

Input & Output

example_1.py โ€” Basic Friendship Network
$ 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
๐Ÿ’ก Note: At timestamp 20190301, when persons 0 and 3 become friends, it connects the final two separate groups: {0,1,5,2} and {3,4}, making everyone acquainted.
example_2.py โ€” Already Connected Small Group
$ Input: logs = [[0,2,0],[1,0,1],[3,0,3],[4,1,2],[7,3,1]], n = 4
โ€บ Output: 3
๐Ÿ’ก Note: After timestamp 3, all 4 people are connected: 0-2-1 and 0-3 form one connected component covering everyone.
example_3.py โ€” Impossible Connection
$ Input: logs = [[9,3,0],[0,2,1],[8,0,1],[1,3,2],[2,2,0],[5,3,1]], n = 4
โ€บ Output: 2
๐Ÿ’ก Note: All people become connected at timestamp 2 when the friendship network forms a complete connected graph.

Constraints

  • 1 โ‰ค n โ‰ค 100
  • 1 โ‰ค logs.length โ‰ค 104
  • logs[i].length == 3
  • 0 โ‰ค timestampi โ‰ค 109
  • 0 โ‰ค xi, yi โ‰ค n - 1
  • xi โ‰  yi
  • All timestamps are distinct
  • All friendships are bidirectional

Visualization

Tap to expand
Social Network Evolution: From Strangers to Connected Communityt=0: Everyone Isolated (6 components)012345t=20190101: First Friends (5 components)012345friendst=20190224: Groups Form (3 components)012345t=20190301: EVERYONE CONNECTED! (1 component)012345๐ŸŽ‰ ANSWER: 20190301 ๐ŸŽ‰Union-Find Magic:โœ“ Track components efficiently (no need to check all connections)โœ“ Union operation decreases component count โœ“ Answer = first time components = 1
Understanding the Visualization
1
Initial State
Everyone starts as isolated individuals (n separate components)
2
First Connections
Early friendships create small groups, reducing total components
3
Group Merging
Strategic friendships merge existing groups into larger networks
4
Final Connection
The last friendship connects all groups into one unified network
Key Takeaway
๐ŸŽฏ Key Insight: Union-Find transforms an O(nยฒm) connectivity problem into an elegant O(m log n) solution by tracking components instead of individual connections!
Asked in
Meta 45 Google 38 Amazon 32 Microsoft 28
89.0K Views
Medium Frequency
~15 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen