You are given activity logs for a ride-sharing app. Each log entry indicates that two riders shared a ride at a certain time, which creates an undirected connection between them (they can “reach” each other either directly or through other riders).
Given:
n
= number of riders, labeled
0..n-1
.
logs
, where each entry is
[timestamp, a, b]
meaning rider
a
and rider
b
shared a ride at
timestamp
.
Return the earliest timestamp at which all riders become mutually connected (i.e., the graph of riders becomes a single connected component). If this never happens, return -1.
logs
may be unsorted.
0 <= a, b < n
,
a != b
.
1e5
logs).
Now suppose riders can also block each other. A block means two riders should be treated as not connected, even if there is a path between them that would otherwise connect them.
You may model this as additional events like [timestamp, "BLOCK", x, y] (and optionally [timestamp, "UNBLOCK", x, y]).
Discuss how you would modify your approach to support block events, and how you would determine the earliest time when all riders are mutually connected under these constraints.