Repository navigation
Expand file tree
/
Copy pathcommunicating_servers.py
More file actions
67 lines (54 loc) · 1.62 KB
/
Copy pathcommunicating_servers.py
File metadata and controls
67 lines (54 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
You are given a two-dimensional matrix containing only ones and zeroes
representing a computer network. Every one in the matrix represents a server
and every zero represents an empty space. Two servers within the network can
communicate if they are either in the same row or the same column.
Return the total number of servers that can communicate with at least one
other server.
Ex: Given the following matrix…
matrix = [
[1, 0],
[1, 0],
], return 2 (both servers are in the same column and can therefore communicate
with one another).
Ex: Given the following matrix…
matrix = [
[1, 0],
[0, 1],
], return 0.
"""
def count_communicating_servers(matrix):
if not matrix or not matrix[0]:
return 0
rows, cols = len(matrix), len(matrix[0])
row_counts = [0] * rows
col_counts = [0] * cols
# Count servers in each row and column
for i in range(rows):
for j in range(cols):
if matrix[i][j] == 1:
row_counts[i] += 1
col_counts[j] += 1
# Count servers that can communicate
communicating_servers = 0
for i in range(rows):
for j in range(cols):
if matrix[i][j] == 1 and (row_counts[i] > 1 or col_counts[j] > 1):
communicating_servers += 1
return communicating_servers
# Test cases
matrix1 = [
[1, 0],
[1, 0]
]
print(count_communicating_servers(matrix1)) # Output: 2
matrix2 = [
[1, 0],
[0, 1]
]
print(count_communicating_servers(matrix2))
matrix3 = [
[1, 1],
[1, 0]
]
print(count_communicating_servers(matrix3))