1101. The Earliest Moment When Everyone Become Friends

Practice The Earliest Moment When Everyone Become Friends for free on NudgeCode: Medium, Graph pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.

There are n people labelled 0 .. n-1. Each logs[i] = [timestamp, x, y] says x and y became friends at timestamp. Friendship is symmetric, and people are acquainted if they are friends or connected through a chain of friends.

Return the earliest timestamp at which every person is acquainted with every other person, or -1 if that never happens. The logs are not necessarily sorted.

Example

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

Constraints