> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# The Earliest Moment When Everyone Become

> Tested Python solution for LeetCode 1101 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 1101, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Union Find](/catalog/topics/union-find), [Sorting](/catalog/topics/sorting). [View on LeetCode](https://leetcode.com/problems/the-earliest-moment-when-everyone-become-friends/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1101   # by problem number
lcpy gen -s the_earliest_moment_when_everyone_become_friends   # by problem name
```

## 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

```
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: After the event at timestamp 20190301, every person becomes acquainted with every other person.
```

```
Input: logs = [[0,2,0],[1,0,1],[3,0,3],[4,1,2],[7,3,1]], n = 4
Output: 3
```

### 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](https://github.com/wislertt/leetcode-py/blob/main/leetcode/the_earliest_moment_when_everyone_become_friends/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/the_earliest_moment_when_everyone_become_friends/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m log m) for sorting the logs
    # Space: O(n)
    def earliest_acq(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]]
                x = parent[x]
            return x

        components = n
        for timestamp, x, y in sorted(logs):
            rx, ry = find(x), find(y)
            if rx == ry:
                continue
            parent[rx] = ry
            components -= 1
            if components == 1:
                return timestamp
        return -1
```

## Complexity

| Time | Space |
| - | - |
| O(m log m) for sorting the logs | O(n) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.