Skip to content

Commit 93dee59

Browse files
committed
added Number of Operations to Make Network Connected (medium)
1 parent 71d3552 commit 93dee59

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
# Number of Operations to Make Network Connected
3+
[Leetcode Link](https://leetcode.com/problems/number-of-operations-to-make-network-connected/)
4+
5+
## Problem:
6+
7+
There are `n` computers numbered from `0` to `n-1` connected by ethernet cables connections forming a network where `connections[i] = [a, b]` represents a connection between computers `a` and `b`. Any computer can reach any other computer directly or indirectly through the network.
8+
9+
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's not possible, return -1.
10+
11+
## Example:
12+
13+
![example](assets/example1.png)
14+
```
15+
Input: n = 4, connections = [[0,1],[0,2],[1,2]]
16+
Output: 1
17+
Explanation: Remove cable between computer 1 and 2 and place between computers 1 and 3.
18+
```
19+
![example](assets/example2.png)
20+
```
21+
Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2],[1,3]]
22+
Output: 2
23+
```
24+
```
25+
Input: n = 6, connections = [[0,1],[0,2],[0,3],[1,2]]
26+
Output: -1
27+
Explanation: There are not enough cables.
28+
```
29+
```
30+
Input: n = 5, connections = [[0,1],[0,2],[3,4],[2,3]]
31+
Output: 0
32+
```
33+
34+
## Note:
11.6 KB
Loading
15 KB
Loading
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from typing import List
2+
from collections import defaultdict
3+
4+
5+
class Solution:
6+
# the main idea is that if number of connections is less than number of computers - 1, then it is not possible to form connections
7+
# else, find how many disjoint graphs there are, and the number of connections that needs to be modified is simply num of disjoint - 1
8+
def makeConnected(self, n: int, connections: List[List[int]]) -> int:
9+
if len(connections) < n-1:
10+
return -1
11+
undirectedGraph = defaultdict(list)
12+
for a, b in connections:
13+
undirectedGraph[a].append(b)
14+
undirectedGraph[b].append(a)
15+
# print(undirectedGraph)
16+
visited = set()
17+
queue = list()
18+
numDisjoint = 0
19+
# visit all computers (even disjoint ones)
20+
for i in range(n):
21+
# if computer i has been visited by BFS, then do not increment number of disjoint graph
22+
if i not in visited:
23+
queue.append(i)
24+
# the ones in the same queue belong to the same graph group
25+
while queue:
26+
current = queue.pop(0)
27+
visited.add(current)
28+
for neighbor in undirectedGraph[current]:
29+
if neighbor not in visited and neighbor not in queue:
30+
queue.append(neighbor)
31+
numDisjoint += 1
32+
return numDisjoint - 1
33+
34+
35+
# test driver
36+
sol = Solution()
37+
n = 9
38+
connections = [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [4, 5], [4, 6], [5, 6]]
39+
print("Output:", sol.makeConnected(n, connections))

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ Languages used: Java and Python
7272
- [Subarray Sum Equals K](Medium/SubarraySumEqualsK)
7373
- [Find Minimum in Rotated Sorted Array](Medium/FindMinInRotatedSortedArray)
7474
- [Maximum Swap](Medium/MaximumSwap)
75+
- [Number of Operations to Make Network Connected](Medium/NumToMakeNetworkConnected)
7576
- Hard
7677
- [Maximum Score Words Formed by Letters](Hard/MaximumScoreWords)
7778
- [Reducing Dishes](Hard/ReducingDishes)

0 commit comments

Comments
 (0)