Count Servers that Communicate

Medium

Topics
ArrayGraphMatrixCounting

You are given a grid of 0s and 1s where 1 marks a server. Two servers communicate if they lie in the same row or the same column. Return the number of servers that can communicate with at least one other server.

Example 1

01010101
alone in its row and column
Two servers talk if they share a row or a column — adjacency does not matter. So a server counts whenever its row or its column holds another one, and a simple row/column tally answers it without any graph search. Here 0 qualify.
2D grid — one row per line.
Input:  grid = [[1,0],[0,1]]
Output: 0

Example 2

01011101
can communicate — counted
Input:  grid = [[1,0],[1,1]]
Output: 3

Constraints

  • 1 <= m, n <= 250
  • grid[i][j] is 0 or 1.
Editorial — intuition, approach, complexity and pitfalls

Intuition

A server communicates if it shares a row or a column with another server. The instinct is to treat this as connected components and flood-fill, but that answers a different question — communication here is a local property, needing only "is there another server in my row, or in my column".

So count servers per row and per column in one pass. Then a server at (r, c) communicates exactly when rowCount[r] > 1 or colCount[c] > 1. Two passes, no graph, no visited set.

Note it is or, not and: one companion in either direction is enough.

Approach

  1. First pass: tally servers per row and per column.
  2. Second pass: count servers whose row tally or column tally exceeds 1.

Dry Run

grid   1 0
       1 1

rowCount = [1, 2]        colCount = [2, 1]

(0,0): row 0 has 1, col 0 has 2  -> col qualifies   count 1
(1,0): row 1 has 2               -> qualifies       count 2
(1,1): row 1 has 2               -> qualifies       count 3

answer 3

grid   1 0
       0 1
rowCount [1,1]  colCount [1,1]
neither server has company in its row or column -> answer 0

Complexity

  • Time — O(m*n): two sweeps of the grid.
  • Space — O(m + n): the two tally arrays. A flood fill would need O(m*n) for a visited grid plus a stack, to compute something these two arrays already settle.

Common Pitfalls

  • Using and instead of or. A server with one companion in its row communicates even if its column holds only itself.
  • Counting a server as its own companion. The tally includes it, so the test is > 1, not > 0.
  • Reaching for union-find or DFS. It gives the right answer and costs more memory for information not needed.
  • Counting rows and columns that contain servers, rather than counting the servers themselves.

Full Solution — all languages

class Solution:
    def countServers(self, grid):
        R, C = len(grid), len(grid[0])
        rowC = [0] * R
        colC = [0] * C
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1:
                    rowC[r] += 1
                    colC[c] += 1
        cnt = 0
        for r in range(R):
            for c in range(C):
                if grid[r][c] == 1 and (rowC[r] > 1 or colC[c] > 1):
                    cnt += 1
        return cnt
SensAI
Beta
Tap the mic to stop voice mode