Find Earliest Fully Connected Time
GoogleSoftware Engineer · OnsiteLast updated
This problem tests your ability to efficiently track the connectivity of a graph as edges are added over time, requiring an understanding of Union-Find data structures and graph theory.
Problem Statement
You are given n computers labeled from 0 to n-1. Initially, no computers are connected. You are provided with a list of edges, where edges[i] = [u, v, t] represents a network cable being connected between computers u and v at time t.
All time values t are unique. A network is considered "fully connected" if there is a path between every pair of computers. Your task is to find the earliest time t at which the network becomes fully connected. If the network never becomes fully connected, return -1.
Function Signature
def earliest_fully_connected_time(n: int, edges: list[list[int]]) -> int:
Constraints
2 <= n <= 10^51 <= len(edges) <= 10^50 <= u, v < n- Time values are positive integers.
Examples
Example 1:
Input: n = 4, edges = [[0, 1, 10], [1, 2, 15], [2, 3, 20]]
Output: 20
Explanation: At t=10, {0, 1} are connected. At t=15, {0, 1, 2} are connected. At t=20, all four nodes are in a single component.
Example 2:
Input: n = 3, edges = [[0, 1, 5], [0, 2, 10]]
Output: 10
Edge Cases:
nis large with very few edges (should return-1).- Disconnected components that never merge.
- Multiple edges between the same nodes.
Hints
Hint 1
Sort the list of edges by their time `t` to process them in chronological order.
Hint 2
Use the Disjoint Set Union (DSU) data structure to keep track of connected components efficiently.
Hint 3
Maintain a counter for the number of connected components; the graph is fully connected when the number of components equals 1.
Follow-up questions
- How would you modify your solution if edges could be removed at specific times?
- What is the time complexity of your solution, and can you optimize the DSU operations?
- How would the logic change if you were asked to return the *latest* possible time an edge could be removed while still keeping the graph fully connected?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.