Count Servers that Communicate - Problem
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 there 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.
Input & Output
Example 1 — Basic Case
$
Input:
grid = [[1,0],[0,1]]
›
Output:
0
💡 Note:
No server can communicate with any other server. Server at (0,0) has no other server in row 0 or column 0. Server at (1,1) has no other server in row 1 or column 1.
Example 2 — Same Row Communication
$
Input:
grid = [[1,0],[1,1]]
›
Output:
3
💡 Note:
Server (1,0) and (1,1) can communicate (same row). Server (0,0) and (1,0) can communicate (same column). All 3 servers can communicate.
Example 3 — Mixed Communication
$
Input:
grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
›
Output:
4
💡 Note:
Servers (0,0) and (0,1) communicate via same row. Servers (1,2) and (2,2) communicate via same column. Server (3,3) is isolated. Total: 4 servers communicate.
Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m ≤ 250
- 1 ≤ n ≤ 250
- grid[i][j] == 0 or 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code