Count Servers that Communicate

You are given a map of a server center as an `m x n` integer matrix `grid`, where `1` means a server sits in that cell and `0` means the cell is empty. Two servers can communicate if they are in the same row or in the same column. Return the number of servers that can communicate with at least one other server.

30:00

Count Servers that Communicate
medium
Free written solution
Topics
Companies

You are given a map of a server center as an m x n integer matrix grid, where 1 means a server sits in that cell and 0 means the cell is empty. Two servers can communicate if they are in the same row or in the same column.

Return the number of servers that can communicate with at least one other server.

Example 1:
Input: {"grid":[[1,0],[0,1]]}
Output: 0
Constraints:
  • m==grid.lengthm == \text{grid.length}

  • n==grid[i].lengthn == \text{grid}[i].\text{length}

  • 1≤m≤2501 \leq m \leq 250

  • 1≤n≤2501 \leq n \leq 250

  • grid[i][j]\text{grid}[i][j] is 00 or 11

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"grid":[[1,0],[0,1]]}

2 servers on a 2x2 grid. First pass: count how many servers each row and each column holds.

right edge = servers per row, bottom edge = servers per column (green when more than 1)

0

0

0

0

servers that communicate

0

pass

1: tally rows[r]++, cols[c]++

Variables
VariableValue
count0
DepthFunction Call
Stack empty
0/6