Count Servers that Communicate (LeetCode 1267): Solution and Explanation

Medium
Array
Depth First search
Breadth First search
Union find
Matrix
Counting

A grid marks servers with 1. Two servers can communicate when they share a row or a column. Return how many servers can communicate with at least one other server.

Intuition

It is tempting to treat this as a graph problem and run union find or DFS over servers. But the question only asks whether each server has at least one partner, not how the groups look.

A server has a partner exactly when its row holds another server or its column holds another server. Whether that partner is next door or at the far edge of the grid does not matter.

So all you need is the number of servers in each row and in each column. Collect those counts in one pass, then a second pass can answer the question for every server in O(1).

Approach: Row and column counts

  1. Create rows of length m and cols of length n, all zeros.
  2. First pass: for every server at (r, c), increment rows[r] and cols[c].
  3. Set count = 0.
  4. Second pass: for every server at (r, c), if rows[r] > 1 or cols[c] > 1, increment count.
  5. Return count.

Step-by-step example

grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
  1. Pass 1 gives rows = [2,1,1,1] and cols = [1,1,2,1].
  2. (0,0): rows[0] = 2, so it can talk along row 0. count = 1. (0,1): same row, count = 2.
  3. (1,2): rows[1] = 1 but cols[2] = 2, so it talks along column 2. count = 3. (2,2) likewise, count = 4.
  4. (3,3): rows[3] = 1 and cols[3] = 1. It is alone and is not counted.
  5. The function returns 4.

Code: Python, Java, C++

1class Solution:
2    def countServers(self, grid: List[List[int]]) -> int:
3        m, n = len(grid), len(grid[0])
4        rows = [0] * m
5        cols = [0] * n
6        for r in range(m):
7            for c in range(n):
8                if grid[r][c] == 1:
9                    rows[r] += 1
10                    cols[c] += 1
11        count = 0
12        for r in range(m):
13            for c in range(n):
14                if grid[r][c] == 1 and (rows[r] > 1 or cols[c] > 1):
15                    count += 1
16        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(m x n)

Two scans over the grid, each doing constant work per cell.

Space

O(m + n)

One counter per row and one per column.

Other approaches

Union find over servers (time O(m x n x alpha), space O(m x n))

Union every server with the others in its row and column, then add up the sizes of components with more than one server. Correct and general, but more code for a question that only needs a yes or no per server.

Check each server's row and column directly (time O(m x n x (m + n)), space O(1))

For every server, scan its whole row and column looking for another 1. Simple, but each server costs O(m + n), which is O(m x n x (m + n)) in a dense grid.

Common mistakes

  • Only checking the four adjacent cells. Servers communicate across any distance in the same row or column.
  • Testing rows[r] > 0 instead of > 1. The count includes the server itself, so a lone server already gives 1.
  • Counting a server twice when it qualifies through both its row and its column. Use one or test per server.

Frequently asked questions

Why is the threshold greater than 1 and not greater than 0?

The row and column counts include the server you are looking at. A count of 1 means the server is the only one there, so it has nobody to talk to.

Do servers need to be adjacent to communicate?

No. Any two servers in the same row or the same column can communicate, regardless of the empty cells between them.

Is a graph search needed?

No. A server needs just one partner, and the row and column counts already tell you whether one exists. Union find would also work but does more than the question asks.

What is the complexity?

O(m x n) time for the two passes and O(m + n) space for the counters.