This repository contains solutions to the problem "Count Servers That Communicate" in C++, Java, JavaScript, Python, and Go. Below is the step-by-step explanation of the logic and approach for each language.
You are given a grid where:
1
represents a server.0
represents an empty space.
The goal is to count the servers that can communicate. Two servers can communicate if:
- They are in the same row or the same column.
We use a two-pass approach:
- First pass: Count the servers in each row and column.
- Second pass: Identify servers that can communicate based on the counts from the first pass.
- Initialize Row and Column Count: Create two arrays to store the number of servers in each row and column.
- First Pass: Traverse the grid and update the row and column count for each server found (
1
). - Second Pass: Traverse the grid again. For each server, check if its row or column count is greater than
1
. If so, it can communicate. Increment the count. - Return the Result: Finally, return the total count of servers that can communicate.
- Initialize Row and Column Count: Use two arrays to store the number of servers in each row and column.
- First Pass: Iterate through the grid, and for every server (
1
), increment the respective row and column counts. - Second Pass: Iterate through the grid again. For every server (
1
), check if its row count or column count is greater than1
. If true, it can communicate, so add to the total count. - Return the Result: Return the count of all communicable servers.
- Initialize Row and Column Count: Create two arrays, one for rows and one for columns, and initialize them to zero.
- First Pass: Use nested loops to traverse the grid. For every server (
1
), increment the row and column counters at the respective indices. - Second Pass: Loop through the grid again. Check for each server (
1
) if the row or column count at its position is greater than1
. If so, increment the result count. - Return the Result: Return the total count of servers that can communicate.
- Initialize Row and Column Count: Use two lists to keep track of the number of servers in each row and column.
- First Pass: Iterate through the grid. For every server (
1
), increment the respective row and column counts. - Second Pass: Iterate through the grid again. For every server (
1
), check if the row count or column count is greater than1
. If true, add it to the total count. - Return the Result: Finally, return the count of servers that can communicate.
- Initialize Row and Column Count: Create slices to store the count of servers in each row and column.
- First Pass: Loop through the grid using nested loops. For every server (
1
), increment the corresponding row and column counts. - Second Pass: Loop through the grid again. For each server (
1
), check if the count in its row or column is greater than1
. If true, increment the total count. - Return the Result: Return the total number of servers that can communicate.
- All implementations follow the same two-pass approach to solve the problem.
- The time complexity of all solutions is O(m × n), where
m
is the number of rows andn
is the number of columns. - The space complexity of all solutions is O(m + n) for storing row and column counts.