Leetcode 1319: Number of Operations to Make Network Connected

grid47
grid47
Exploring patterns and algorithms
Jun 28, 2024 6 min read

You are given a network of computers, each labeled from 0 to n-1, and a list of direct connections between these computers. The network is initially connected in some way, but some computers are not directly connected. You can remove cables from existing direct connections and reconnect them between disconnected computers to minimize the number of operations required to make the entire network fully connected. The task is to determine the minimum number of operations needed. If it’s not possible to connect all the computers, return -1.
Problem
Approach
Steps
Complexity
Input: The input consists of two parts: the number of computers n, and a list of connections between computers. Each connection is represented as a pair of integers, where each pair [ai, bi] indicates that computer ai is directly connected to computer bi.
Example: For n = 5 and connections = [[0,1], [0,2], [3,4]], we have a network with 5 computers, and only the first three computers are connected directly.
Constraints:
• 1 <= n <= 10^5
• 1 <= connections.length <= min(n * (n - 1) / 2, 10^5)
• connections[i].length == 2
• 0 <= ai, bi < n
• ai != bi
• There are no repeated connections.
Output: The output should be the minimum number of cable reconfigurations needed to make the entire network connected. If it is impossible, return -1.
Example: For n = 5, connections = [[0,1], [0,2], [3,4]], the output would be 1, as we can move one cable between disconnected components to connect all the computers.
Constraints:
Goal: The goal is to determine if all computers can be connected with the given cables, and if so, calculate the minimum number of moves needed to achieve full connectivity.
Steps:
• 1. Use a union-find (disjoint-set) data structure to track the connected components.
• 2. Iterate through the given connections and join the connected computers.
• 3. Count the number of disconnected components.
• 4. If there are fewer connections than the number of components minus one, return -1 (impossible to connect).
• 5. Otherwise, return the number of reconfigurations required, which is the number of additional cables needed to connect all components.
Goal: The problem guarantees valid input values and specifies the upper limit of n and connections.
Steps:
• 1 <= n <= 10^5
• 1 <= connections.length <= min(n * (n - 1) / 2, 10^5)
• No two computers are connected by more than one cable.
Assumptions:
• The network may initially have some connected components, and you need to figure out how to connect them all.
• You can freely remove and place cables between disconnected computers.
Input: For n = 4 and connections = [[0,1], [0,2], [1,2]], the output is 1.
Explanation: There are 4 computers, and the given connections form 3 connected components: {0, 1, 2}, {3}. You need to add one cable to connect computer 3 to the existing network.

Input: For n = 6 and connections = [[0,1], [0,2], [0,3], [1,2], [1,3]], the output is 2.
Explanation: There are 6 computers, and the given connections form 1 connected component for computers {0, 1, 2, 3}, leaving computers {4, 5} disconnected. You need 2 additional connections to make the network fully connected.

Input: For n = 6 and connections = [[0,1], [0,2], [0,3], [1,2]], the output is -1.
Explanation: The given connections only form 4 connected computers, leaving 2 disconnected computers. Since there are not enough cables to connect them, it is impossible to connect the entire network.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus