{x}
blog image

Count Servers that Communicate

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.length
  • n == grid[i].length
  • 1 <= m <= 250
  • 1 <= n <= 250
  • grid[i][j] == 0 or 1

Solution Explanation: Counting Servers that Communicate

This problem asks to find the number of servers that can communicate with at least one other server. Two servers communicate if they are on the same row or column. The most efficient solution uses a counting approach.

Approach:

  1. Count Row and Column Servers: We first iterate through the grid and count the number of servers in each row and each column. We store these counts in row and col arrays respectively.

  2. Check Communication: We iterate through the grid again. For each server (represented by a 1 in the grid), we check if either the count of servers in its row (row[i]) or the count of servers in its column (col[j]) is greater than 1. If either condition is true, it means the server can communicate with at least one other server, and we increment the ans counter.

  3. Return Count: Finally, we return the ans which represents the total number of servers that can communicate.

Time Complexity Analysis:

The solution involves two nested loops to iterate through the grid (m x n size). Therefore, the time complexity is O(m*n), where 'm' is the number of rows and 'n' is the number of columns. This is linear with respect to the size of the input grid.

Space Complexity Analysis:

We use two arrays, row and col, to store the counts of servers in each row and column. The size of these arrays is m and n respectively. Therefore, the space complexity is O(m+n), which is linear with respect to the dimensions of the input grid. This is relatively small compared to the input size.

Code Examples:

The provided code examples in Python, Java, C++, Go, and TypeScript all implement this counting approach. They differ slightly in syntax but follow the same fundamental logic:

  • Initialization: Arrays row and col are initialized to store row and column counts.
  • Counting Phase: The first nested loop populates row and col.
  • Communication Check: The second nested loop checks communication and updates the ans counter.
  • Return Value: The function returns ans.

The code is concise and efficiently solves the problem within the stated time and space complexity bounds. No more sophisticated data structures or algorithms (like Union Find) are necessary for this optimal solution.