Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
Return the number of servers that communicate with any other server.
Example 1:
Input: grid = [[1,0],[0,1]] Output: 0 Explanation: No servers can communicate with others.
Example 2:
Input: grid = [[1,0],[1,1]] Output: 3 Explanation: All three servers can communicate with at least one other server.
Example 3:
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]] Output: 4 Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
Constraints:
m == grid.lengthn == grid[i].length1 <= m <= 2501 <= n <= 250grid[i][j] == 0 or 1Problem summary: You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column. Return the number of servers that communicate with any other server.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Union-Find
[[1,0],[0,1]]
[[1,0],[1,1]]
[[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1267: Count Servers that Communicate
class Solution {
public int countServers(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] row = new int[m];
int[] col = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
row[i] += grid[i][j];
col[j] += grid[i][j];
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1 && (row[i] > 1 || col[j] > 1)) {
++ans;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #1267: Count Servers that Communicate
func countServers(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
row, col := make([]int, m), make([]int, n)
for i := range grid {
for j, x := range grid[i] {
row[i] += x
col[j] += x
}
}
for i := range grid {
for j, x := range grid[i] {
if x == 1 && (row[i] > 1 || col[j] > 1) {
ans++
}
}
}
return
}
# Accepted solution for LeetCode #1267: Count Servers that Communicate
class Solution:
def countServers(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
row = [0] * m
col = [0] * n
for i in range(m):
for j in range(n):
row[i] += grid[i][j]
col[j] += grid[i][j]
return sum(
grid[i][j] and (row[i] > 1 or col[j] > 1)
for i in range(m)
for j in range(n)
)
// Accepted solution for LeetCode #1267: Count Servers that Communicate
impl Solution {
pub fn count_servers(grid: Vec<Vec<i32>>) -> i32 {
let m = grid.len();
let n = grid[0].len();
let mut row = vec![0; m];
let mut col = vec![0; n];
for i in 0..m {
for j in 0..n {
row[i] += grid[i][j];
col[j] += grid[i][j];
}
}
let mut ans = 0;
for i in 0..m {
for j in 0..n {
if grid[i][j] == 1 && (row[i] > 1 || col[j] > 1) {
ans += 1;
}
}
}
ans
}
}
// Accepted solution for LeetCode #1267: Count Servers that Communicate
function countServers(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const row = new Array(m).fill(0);
const col = new Array(n).fill(0);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
row[i] += grid[i][j];
col[j] += grid[i][j];
}
}
let ans = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1 && (row[i] > 1 || col[j] > 1)) {
ans++;
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Track components with a list or adjacency matrix. Each union operation may need to update all n elements’ component labels, giving O(n) per union. For n union operations total: O(n²). Find is O(1) with direct lookup, but union dominates.
With path compression and union by rank, each find/union operation takes O(α(n)) amortized time, where α is the inverse Ackermann function — effectively constant. Space is O(n) for the parent and rank arrays. For m operations on n elements: O(m × α(n)) total.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.