Count Servers that Communicate (LeetCode 1267): Solution and Explanation
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
- Create
rowsof length m andcolsof length n, all zeros. - First pass: for every server at
(r, c), incrementrows[r]andcols[c]. - Set
count = 0. - Second pass: for every server at
(r, c), ifrows[r] > 1orcols[c] > 1, incrementcount. - Return
count.
Step-by-step example
grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
- Pass 1 gives rows = [2,1,1,1] and cols = [1,1,2,1].
- (0,0): rows[0] = 2, so it can talk along row 0. count = 1. (0,1): same row, count = 2.
- (1,2): rows[1] = 1 but cols[2] = 2, so it talks along column 2. count = 3. (2,2) likewise, count = 4.
- (3,3): rows[3] = 1 and cols[3] = 1. It is alone and is not counted.
- 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 count1class Solution {
2 public int countServers(int[][] grid) {
3 int m = grid.length, n = grid[0].length;
4 int[] rows = new int[m];
5 int[] cols = new int[n];
6 for (int r = 0; r < m; r++) {
7 for (int c = 0; c < n; c++) {
8 if (grid[r][c] == 1) {
9 rows[r]++;
10 cols[c]++;
11 }
12 }
13 }
14 int count = 0;
15 for (int r = 0; r < m; r++) {
16 for (int c = 0; c < n; c++) {
17 if (grid[r][c] == 1 && (rows[r] > 1 || cols[c] > 1)) {
18 count++;
19 }
20 }
21 }
22 return count;
23 }
24}1class Solution {
2public:
3 int countServers(vector<vector<int>>& grid) {
4 int m = grid.size(), n = grid[0].size();
5 vector<int> rows(m, 0), cols(n, 0);
6 for (int r = 0; r < m; r++) {
7 for (int c = 0; c < n; c++) {
8 if (grid[r][c] == 1) {
9 rows[r]++;
10 cols[c]++;
11 }
12 }
13 }
14 int count = 0;
15 for (int r = 0; r < m; r++) {
16 for (int c = 0; c < n; c++) {
17 if (grid[r][c] == 1 && (rows[r] > 1 || cols[c] > 1)) {
18 count++;
19 }
20 }
21 }
22 return count;
23 }
24};Time and space complexity
O(m x n)
Two scans over the grid, each doing constant work per cell.
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] > 0instead 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
ortest 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.