Ctrl K

When Everyone Is Linked

Medium
A conference has people attendees numbered from 0 to people - 1. The log meetings holds records [t, x, y]: at time t, attendees x and y got to know each other. The records are in no particular order, and the same pair may appear more than once. Acquaintance is mutual. Two attendees are linked if they know each other directly or through a chain of mutual acquaintances. Return the earliest time at which every attendee is linked to every other one. If that never happens, return -1.
Example 1
meetings=[[9, 1, 2], [3, 0, 1], [15, 2, 3], [12, 0, 2]], people=4 → 15

At time 3 people 0 and 1 are linked, at time 9 person 2 joins them, the meeting at 12 changes nothing, and at 15 person 3 is linked in: everyone is in one group.

Example 2
meetings=[[1, 0, 1], [2, 2, 3]], people=4 → -1

Two separate groups remain, {0, 1} and {2, 3}, so the answer is -1.

Example 3
meetings=[[50, 0, 2], [20, 1, 2], [70, 0, 1]], people=3 → 50
Constraints
  • 2 ≤ people ≤ 100
  • 1 ≤ len(meetings) ≤ 10⁵
  • len(meetings[i]) == 3
  • 0 ≤ t ≤ 10⁹, and all times t are distinct
  • 0 ≤ x, y ≤ people - 1, x ≠ y
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMerge Contact Cards MediumRearrange Through Links MediumSwapping Close Values MediumCell Levels by Rows and Columns HardScooter Trips Under a Road Limit Hard
loading Python…
Input
meetings=[[9, 1, 2], [3, 0, 1], [15, 2, 3], [12, 0, 2]], people=4
Expected
15

Run checks the first 3 tests in your browser. Submit checks all 86 tests on the server, hidden ones included.