As reported: Google L3 onsite, round 3 (2025): an array of pairs of friends with the time they become friends is given, plus two people
F1andF2; find the minimum time at which they become friends. A graph with a DP array of minimum times was coded.
There are n people 0 … n − 1. Each log entry [time, a, b] says that a and b become friends at time. Friendship is transitive for this question: two people are connected once a chain of friendships links them.
Given two different people f1 and f2, return the earliest time at which f1 and f2 are connected, or -1 if they never are.
Example
n = 4, logs = [[5,0,1], [3,2,3], [8,1,2], [10,0,3]]
f1 = 0, f2 = 3 -> 8 at 3: {2,3}; at 5: {0,1}; at 8 the 1–2 friendship links them
f1 = 0, f2 = 1 -> 5
f1 = 0, f2 = 3, logs without [8,1,2] -> 10
Clarifications
- Logs are not sorted by time; times may repeat.
f1 ≠ f2.
Constraints: 2 ≤ n ≤ 10^5; 1 ≤ logs.length ≤ 2·10^5; 0 ≤ time ≤ 10^9; a ≠ b.
Code runner format: the first line is n L f1 f2, then L lines time a b. Implement earliestConnection(n, logs, f1, f2); the harness prints the result.