LeetCode Problem

How to Solve Count Servers that Communicate

In this problem, you're given a grid representing a server center, and the task is to count how many servers can communicate with any other server. Servers communicate if they share the same row or column. The solution leverages an array-based approach combined with Depth-First Search (DFS) to efficiently determine server connectivity across the grid.

GhostInterview Help

Need help with Count Servers that Communicate without spending extra time grinding it?

GhostInterview can read Count Servers that Communicate from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1267Array plus Depth-First SearchReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array plus Depth-First Search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

In this problem, you're given a grid representing a server center, and the task is to count how many servers can communicate with any other server. Servers communicate if they share the same row or column. The solution leverages an array-based approach combined with Depth-First Search (DFS) to efficiently determine server connectivity across the grid.

Problem Statement

You are given a m * n integer matrix grid representing a server center. Each cell in the grid contains either a 1 (indicating a server) or a 0 (indicating no server). Two servers can communicate if they are in the same row or column, and you are asked to return the number of servers that can communicate with at least one other server.

For example, if a server is isolated, it cannot communicate with any other servers. Servers that are aligned in the same row or column can communicate with each other. The task is to determine how many servers in the given grid can communicate with at least one other server.

Examples

Example 1

Input: grid = [[1,0],[0,1]]

Output: 0

No servers can communicate with others.

Example 2

Input: grid = [[1,0],[1,1]]

Output: 3

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

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.length
  • n == grid[i].length
  • 1 <= m <= 250
  • 1 <= n <= 250
  • grid[i][j] == 0 or 1

Solution Approach

Count Servers in Rows and Columns

First, count the number of servers in each row and column. This allows us to determine which servers have at least one other server in the same row or column. Storing these counts in arrays for rows and columns provides a quick way to identify servers that can communicate.

Traverse the Grid

Next, traverse the grid cell by cell. For each server found (grid[i][j] == 1), check the corresponding row and column counts. If the count of servers in either the row or the column is greater than one, that server can communicate with another.

Efficient Counting with DFS or BFS

You can use Depth-First Search (DFS) or Breadth-First Search (BFS) to mark connected servers and avoid counting the same server multiple times. This ensures that only servers that are isolated or grouped together in their row or column are considered.

Complexity Analysis

MetricValue
TimeO(m \times n)
SpaceO(1)

The time complexity of this approach is O(m * n) because we need to traverse the grid and count the servers in each row and column. The space complexity is O(1), as we only need a few arrays for counting, independent of the grid size.

What Interviewers Usually Probe

  • Look for an efficient grid traversal approach, possibly utilizing row and column counters.
  • Check if the candidate correctly applies DFS or BFS to avoid redundant counts.
  • See if the candidate focuses on optimizing the traversal to avoid unnecessary recalculations.

Common Pitfalls or Variants

Common pitfalls

  • Not considering servers in isolated rows or columns which can't communicate with others.
  • Overcomplicating the solution by attempting to check each server pair individually instead of using row and column counts.
  • Failing to optimize the traversal, leading to unnecessary checks for already visited servers.

Follow-up variants

  • Grid with all cells having servers, where every server should communicate with every other server.
  • Grid with no servers, where the answer should be zero.
  • Sparse grids with servers scattered in isolated rows and columns.

How GhostInterview Helps

  • GhostInterview breaks down the problem into key patterns, helping you recognize that row and column counts are crucial to solve this problem efficiently.
  • It provides a step-by-step breakdown of using DFS/BFS to mark connected servers, guiding you on how to optimize the traversal process.
  • The solver ensures that you avoid common pitfalls, such as double-counting or missing isolated servers, by providing insights into how to optimize grid traversal.

Topic Pages

FAQ

What is the key pattern to solve 'Count Servers that Communicate'?

The key pattern involves efficiently counting the number of servers in each row and column, then using that information to determine which servers can communicate.

How do I avoid counting the same server multiple times?

By using DFS or BFS, you can mark servers that have been visited, ensuring that each server is only counted once in its respective group.

Can I solve this problem without using DFS or BFS?

Yes, you can use an array-based approach to count servers in each row and column and directly compute the result without DFS or BFS.

How do row and column counts help in solving the problem?

Row and column counts allow you to quickly identify if a server has another server in the same row or column, making the traversal more efficient.

What happens if all servers are isolated?

If all servers are isolated, the output will be zero, as no server can communicate with any other server.

GhostInterview Solver

Need direct help with Count Servers that Communicate instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Count Servers that Communicate from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.