filmov
tv
1267. Count Servers that Communicate | leetCode Problem of the day I Java I dsa I Ad-astra #dsa

Показать описание
Here's an explanation and logic of the code for the countServers function:
Problem:
Given a 2D grid where 1 represents a server and 0 represents an empty space, we need to find the number of servers that communicate with at least one other server.
Two servers can communicate if they are on the same row or the same column.
Logic:
Initialization:
Create arrays rowCnt and colCnt to store the number of servers in each row and column, respectively.
Initialize totalServers to 0.
First Pass:
Iterate through each cell in the grid:
If a server is found (grid[i][j] == 1):
Increment the count of servers in the corresponding row (rowCnt[i])
Increment the count of servers in the corresponding column (colCnt[j])
Increment the total number of servers (totalServers)
Second Pass:
Iterate through each cell in the grid again:
If a server is found (grid[i][j] == 1) and:
The number of servers in its row (rowCnt[i]) is 1
The number of servers in its column (colCnt[j]) is 1
This indicates the server is isolated and cannot communicate with any other server.
Decrement the totalServers count.
Return Result:
Return the final value of totalServers.
Explanation:
The code efficiently counts the total number of servers in the first pass.
The second pass identifies and excludes the isolated servers that cannot communicate with others.
This approach avoids unnecessary calculations and provides an optimized solution to the problem.
In essence, the code determines the number of servers that are not "stand-alone" by checking if they have at least one neighbor in their row or column.
Problem:
Given a 2D grid where 1 represents a server and 0 represents an empty space, we need to find the number of servers that communicate with at least one other server.
Two servers can communicate if they are on the same row or the same column.
Logic:
Initialization:
Create arrays rowCnt and colCnt to store the number of servers in each row and column, respectively.
Initialize totalServers to 0.
First Pass:
Iterate through each cell in the grid:
If a server is found (grid[i][j] == 1):
Increment the count of servers in the corresponding row (rowCnt[i])
Increment the count of servers in the corresponding column (colCnt[j])
Increment the total number of servers (totalServers)
Second Pass:
Iterate through each cell in the grid again:
If a server is found (grid[i][j] == 1) and:
The number of servers in its row (rowCnt[i]) is 1
The number of servers in its column (colCnt[j]) is 1
This indicates the server is isolated and cannot communicate with any other server.
Decrement the totalServers count.
Return Result:
Return the final value of totalServers.
Explanation:
The code efficiently counts the total number of servers in the first pass.
The second pass identifies and excludes the isolated servers that cannot communicate with others.
This approach avoids unnecessary calculations and provides an optimized solution to the problem.
In essence, the code determines the number of servers that are not "stand-alone" by checking if they have at least one neighbor in their row or column.