1319. Number of Operations to Make Network Connected LeetCode Solution

In this guide, you will get 1319. Number of Operations to Make Network Connected LeetCode Solution with the best time and space complexity. The solution to Number of Operations to Make Network Connected problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Number of Operations to Make Network Connected solution in C++
  4. Number of Operations to Make Network Connected solution in Java
  5. Number of Operations to Make Network Connected solution in Python
  6. Additional Resources
1319. Number of Operations to Make Network Connected LeetCode Solution image

Problem Statement of Number of Operations to Make Network Connected

There are n computers numbered from 0 to n – 1 connected by ethernet cables connections forming a network where connections[i] = [ai, bi] represents a connection between computers ai and bi. Any computer can reach any other computer directly or indirectly through the network.
You are given an initial computer network connections. You can extract certain cables between two directly connected computers, and place them between any pair of disconnected computers to make them directly connected.
Return the minimum number of times you need to do this in order to make all the computers connected. If it is not possible, return -1.

Example 1:

Input: n = 4, connections = [[0,1],[0,2],[1,2]]
Output: 1
Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.

Example 2:

Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
Output: 2

Example 3:

Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
Output: -1
Explanation: There are not enough cables.

See also  3012. Minimize Length of Array Using Operations LeetCode Solution

Constraints:

1 <= n <= 105
1 <= connections.length <= min(n * (n – 1) / 2, 105)
connections[i].length == 2
0 <= ai, bi < n
ai != bi
There are no repeated connections.
No two computers are connected by more than one cable.

Complexity Analysis

  • Time Complexity: O(|V| + |E|), where |E| = |\texttt{connections}|
  • Space Complexity: O(|V| + |E|)

1319. Number of Operations to Make Network Connected LeetCode Solution in C++

class Solution {
 public:
  int makeConnected(int n, vector<vector<int>>& connections) {
    // To connect n nodes, we need at least n - 1 edges
    if (connections.size() < n - 1)
      return -1;

    int numOfConnected = 0;
    vector<vector<int>> graph(n);
    unordered_set<int> seen;

    for (const vector<int>& connection : connections) {
      const int u = connection[0];
      const int v = connection[1];
      graph[u].push_back(v);
      graph[v].push_back(u);
    }

    for (int i = 0; i < n; ++i)
      if (seen.insert(i).second) {
        dfs(graph, i, seen);
        ++numOfConnected;
      }

    return numOfConnected - 1;
  }

 private:
  void dfs(const vector<vector<int>>& graph, int u, unordered_set<int>& seen) {
    for (const int v : graph[u])
      if (seen.insert(v).second)
        dfs(graph, v, seen);
  }
};
/* code provided by PROGIEZ */

1319. Number of Operations to Make Network Connected LeetCode Solution in Java

class Solution {
  public int makeConnected(int n, int[][] connections) {
    // To connect n nodes, we need at least n - 1 edges
    if (connections.length < n - 1)
      return -1;

    int numOfConnected = 0;
    List<Integer>[] graph = new List[n];
    Set<Integer> seen = new HashSet<>();

    for (int i = 0; i < n; ++i)
      graph[i] = new ArrayList<>();

    for (int[] connection : connections) {
      final int u = connection[0];
      final int v = connection[1];
      graph[u].add(v);
      graph[v].add(u);
    }

    for (int i = 0; i < n; ++i)
      if (seen.add(i)) {
        dfs(graph, i, seen);
        ++numOfConnected;
      }

    return numOfConnected - 1;
  }

  private void dfs(List<Integer>[] graph, int u, Set<Integer> seen) {
    for (final int v : graph[u])
      if (seen.add(v))
        dfs(graph, v, seen);
  }
}
// code provided by PROGIEZ

1319. Number of Operations to Make Network Connected LeetCode Solution in Python

N/A
# code by PROGIEZ

Additional Resources

See also  3453. Separate Squares I LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.