Count Servers that Communicate
Medium
Topics
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
Input: grid = [[1,0],[0,1]] Output: 0
Example 2
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
- First pass: tally servers per row and per column.
- 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 0Complexity
- 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 cntimport java.util.*;
class Solution {
public int countServers(int[][] grid) {
int R = grid.length, C = grid[0].length;
int[] rowC = new int[R], colC = new int[C];
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) if (grid[r][c] == 1) {
rowC[r]++;
colC[c]++;
}
int cnt = 0;
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) if (grid[r][c] == 1 && (rowC[r] > 1 || colC[c] > 1)) cnt++;
return cnt;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int countServers(vector<vector<int>>& grid) {
int R = grid.size(), C = grid[0].size();
vector<int> rowC(R, 0), colC(C, 0);
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) if (grid[r][c] == 1) {
rowC[r]++;
colC[c]++;
}
int cnt = 0;
for (int r = 0; r < R; r++) for (int c = 0; c < C; c++) if (grid[r][c] == 1 && (rowC[r] > 1 || colC[c] > 1)) cnt++;
return cnt;
}
};/**
* @param {number[][]} grid
* @return {number}
*/ var countServers = function(grid) {
const R = grid.length, C = grid[0].length;
const rowC = new Array(R).fill(0), colC = new Array(C).fill(0);
for (let r = 0; r < R; r++) for (let c = 0; c < C; c++) if (grid[r][c] === 1) {
rowC[r]++;
colC[c]++;
}
let cnt = 0;
for (let r = 0; r < R; r++) for (let c = 0; c < C; c++) if (grid[r][c] === 1 && (rowC[r] > 1 || colC[c] > 1)) cnt++;
return cnt;
};