FastPrepCount Communicating Servers

Count Communicating Servers

Fox Corporation logoFox Corporation● MediumFULLTIMEPHONE SCREEN
Learn

Problem statement

A binary matrix represents servers: grid[r][c] = 1 means a server is present. Two servers can communicate when they share a row or column.

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

Function

countCommunicatingServers(grid: int[][]) → int

Examples

Example 1

grid = [[1,0],[0,1]]return = 0

Neither server shares a row or column.

Example 2

grid = [[1,0],[1,1]]return = 3

All three servers communicate.

Example 3

grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0]]return = 4

Every server belongs to a shared row or column.

Constraints

  • 0 <= rows, columns <= 1000.
  • Every cell is 0 or 1.

More Fox Corporation problems

See Fox Corporation hiring insights
public int countCommunicatingServers(int[][] grid) {
    // write your code here
}
grid[[1,0],[0,1]]
expected0
Checking account…