LeetCode, 𝐃𝐚𝐲-23/365-DSA-Coding 𝐉𝐨𝐮𝐫𝐧𝐞𝐲…Google Prep…

Aaqib Ali
3 min readJan 23, 2025

--

Today I Learned This Problem: 1267. Count Servers that Communicate

Problem Overview

The challenge here is to count the number of servers in a 2D grid that can communicate with at least one other server. A server is represented by 1 in the grid, and it can communicate if another server exists in the same row or column.

To solve this, we need to check each server’s row and column to see if it shares them with another server. This requires careful grid traversal and efficient counting.

How I Solved It

The solution involves two main steps:

  1. Count how many servers exist in each row and column.
  2. Go through the grid again to check if each server is in a row or column with more than one server. If it is, count it as communicative.

Steps in Detail

Step 1: Count Servers

  • Go through each row and column of the grid.
  • Count how many servers are present in every row and column.
  • Save these counts in two separate arrays.

Step 2: Check Communicative Servers

  • Loop through the grid again.
  • For each server (1 in the grid), check if its row or column has more than one server.
  • If it does, add it to the result.

This approach ensures we calculate everything efficiently without rechecking the same values multiple times.

The Code

class Solution:
def countServers(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
row_count = [sum(row) for row in grid]
col_count = [sum(grid[r][c] for r in range(m)) for c in range(n)]
return sum(
grid[r][c] and (row_count[r] > 1 or col_count[c] > 1)
for r in range(m)
for c in range(n)
)

What I Learned

  1. Focus on Precomputing Values
    I realized that counting rows and columns beforehand saves a lot of time when checking communicative servers later. This is much faster than repeatedly counting servers during the second loop.
  2. Keep it Simple
    It’s easy to overcomplicate grid problems by adding unnecessary conditions or data structures. By breaking the task into two clear steps, the solution becomes simpler and easier to follow.
  3. Think About Efficiency
    The solution works in O(m⋅n)O(m \cdot n)O(m⋅n), where mmm is the number of rows and nnn is the number of columns. This makes it efficient even for larger grids.

Why This Matters

This type of problem shows up in many real-life scenarios:

  • Network Optimization
    In communication networks, identifying which nodes (servers) can communicate is essential for optimizing data flow.
  • Grid-Based Systems
    This approach can be applied to problems in logistics, traffic control, or resource allocation, where grids represent physical spaces.
  • Data Analysis
    Understanding relationships in datasets often involves matrix or grid representations, just like this problem.

Wrapping Up

The “Count Servers That Communicate” problem was a great way to practice grid traversal and efficient counting. By breaking the problem into manageable steps and keeping the solution clear and direct, I was able to solve it in a straightforward and efficient way.

--

--

No responses yet