#1267
Count Servers that Communicate
pupil · 515 · lc medium +28 · verified · 73.5% accepted · 1,910 likes · top 84%
Description
You are given an m x n integer matrix grid representing a server layout, where 1 indicates a server and 0 an empty cell. Two servers can communicate if they share the same row or the same column.
Return the total number of servers that can communicate with at least one 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.
Code
1
2
3