You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
Return the number of servers that communicate with any other server.
Example 1:

Input: grid = [[1,0],[0,1]] Output: 0 Explanation: No servers can communicate with others.
Example 2:

Input: grid = [[1,0],[1,1]] Output: 3 Explanation: All three servers can communicate with at least one other server.
Example 3:

Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] Output: 4 Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
Constraints:
m == grid.lengthn == grid[i].length1 <= m <= 2501 <= n <= 250grid[i][j] == 0 or 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The core idea is to check every server, one at a time. For each server, we will look at all other servers and see if they can communicate with each other.
Here's how the algorithm would work step-by-step:
def count_servers_that_communicate_brute_force(grid):
rows = len(grid)
columns = len(grid[0]) if rows > 0 else 0
communicating_servers_count = 0
# Iterate through each server in the grid
for row_index in range(rows):
for column_index in range(columns):
if grid[row_index][column_index] == 1:
can_communicate = False
# Check against every other server in the grid
for other_row_index in range(rows):
for other_column_index in range(columns):
# Skip checking the server against itself
if row_index == other_row_index and column_index == other_column_index:
continue
# Check if the other server exists
if grid[other_row_index][other_column_index] == 1:
# Check if they share a row or column
if row_index == other_row_index or column_index == other_column_index:
# If they share a row or column, communication is possible
can_communicate = True
break
if can_communicate:
break
# Increment count if server can communicate with others
if can_communicate:
communicating_servers_count += 1
return communicating_servers_countTo efficiently find the number of servers that can communicate, the key idea is to focus on the rows and columns where servers are located. We'll count servers by examining rows and columns, not individual servers, to avoid redundant checks. A server can communicate if there's at least one other server in its row or column.
Here's how the algorithm would work step-by-step:
def count_servers(grid):
number_of_rows = len(grid)
number_of_columns = len(grid[0]) if number_of_rows > 0 else 0
row_counts = [0] * number_of_rows
column_counts = [0] * number_of_columns
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
if grid[row_index][column_index] == 1:
row_counts[row_index] += 1
column_counts[column_index] += 1
communicating_servers_count = 0
# Iterate through each cell to find communicating servers
for row_index in range(number_of_rows):
for column_index in range(number_of_columns):
if grid[row_index][column_index] == 1:
# A server communicates if others share its row or column.
if row_counts[row_index] > 1 or column_counts[column_index] > 1:
communicating_servers_count += 1
# Return the count of servers that can communicate
return communicating_servers_count| Case | How to Handle |
|---|---|
| Null or empty grid input | Return 0 immediately as there are no servers to count. |
| Grid with only one row or one column | Iterate through the single row or column and count the servers (value of 1) since they inherently communicate within that row/column. |
| Grid where all servers are isolated (no communication possible) | The algorithm should correctly return 0, as no rows or columns share multiple servers. |
| Grid where all cells contain servers (all 1s) | The solution should return the total number of cells in the grid since every server communicates with every other server within their row and column. |
| Grid with very large dimensions (large number of rows and columns) | Ensure the row and column count operations have acceptable time complexity; using O(m*n) space can improve time complexity if acceptable. |
| Integer overflow potential when summing server counts in large grids | Use a data type capable of holding large counts, such as long in Java/C++ or a similar large integer type. |
| Input grid contains values other than 0 or 1 | Validate the input and either throw an exception or treat any value other than 1 as 0. |
| A row or column contains only one server | The solution needs to exclude these lone servers when counting communicating servers since they need at least two servers to communicate. |