LeetCode 1101, Medium. Topics: Array, Union Find, Sorting. View on LeetCode.
Generate this problem as a practice environment: tested reference solution, 12 parametrized pytest cases, and a playground notebook:
Problem
There are n people in a social group labeled from 0 to n - 1. You are given an array logs where logs[i] = [timestampi, xi, yi] indicates that xi and yi will be friends at the time timestampi.
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.
Examples
Constraints
- 2 <= n <= 100
- 1 <= logs.length <= 10^4
- logs[i].length == 3
- 0 <= timestampi <= 10^9
- 0 <= xi, yi <= n - 1
- xi != yi
- All the values timestampi are unique.
- All the pairs (xi, yi) occur at most one time in the input.
Solution
Reference implementation from solution.py on GitHub, full suite in test_solution.py:
Complexity
NeetCode All. Last modified on September 7, 2026