0
Leetcode Problem 1101. The Earliest Moment When Everyone Become Friends
1101. The Earliest Moment When Everyone Become Friends
AI Mock Interview
Leetcode Solutions
Union-Find (Disjoint-Set) Approach
Solution Idea
Algorithm Steps
Code Implementation
Complexity Analysis
Sort the logs by their timestamps.
Initialize the Union-Find data structure with
n
elements, each in its own set.
Iterate over the sorted logs.
For each log, use the
union
operation to merge the sets containing the two individuals.
If the
union
operation actually merges two sets, decrement the count of disjoint sets.
Once the count of disjoint sets is 1, return the timestamp of the current log.
If the end of the logs is reached and the count of disjoint sets is greater than 1, return -1.
Breadth-First Search (BFS) Approach
Ask Question
Programming Language
Purpose:
General Question
Debug My Code
image/screenshot of info
(optional)
[+]
Full Screen
Loading...
Get Answer
Suggested Answer
Answer
Full Screen
Copy Answer Code
Loading...