class Solution:
def countServers(self, grid):
from collections import Counter
rows = len(grid)
cols = len(grid[0])
row_count = Counter()
col_count = Counter()
# Count servers in each row and column
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
row_count[r] += 1
col_count[c] += 1
# Count servers that can communicate
result = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
if row_count[r] > 1 or col_count[c] > 1:
result += 1
return result
class Solution {
public:
int countServers(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
vector<int> row_count(rows, 0), col_count(cols, 0);
// Count servers in each row and column
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
if (grid[r][c] == 1) {
row_count[r]++;
col_count[c]++;
}
}
}
// Count servers that can communicate
int result = 0;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
if (grid[r][c] == 1) {
if (row_count[r] > 1 || col_count[c] > 1) {
result++;
}
}
}
}
return result;
}
};
class Solution {
public int countServers(int[][] grid) {
int rows = grid.length;
int cols = grid[0].length;
int[] rowCount = new int[rows];
int[] colCount = new int[cols];
// Count servers in each row and column
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 1) {
rowCount[r]++;
colCount[c]++;
}
}
}
// Count servers that can communicate
int result = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 1) {
if (rowCount[r] > 1 || colCount[c] > 1) {
result++;
}
}
}
}
return result;
}
}
var countServers = function(grid) {
const rows = grid.length;
const cols = grid[0].length;
const rowCount = new Array(rows).fill(0);
const colCount = new Array(cols).fill(0);
// Count servers in each row and column
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 1) {
rowCount[r]++;
colCount[c]++;
}
}
}
// Count servers that can communicate
let result = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 1) {
if (rowCount[r] > 1 || colCount[c] > 1) {
result++;
}
}
}
}
return result;
};
You are given a 2D grid representing a network of servers. Each cell in the grid contains either a 1
(representing a server) or a 0
(representing an empty space).
Two servers can communicate if they are located on the same row or the same column.
Your task is to count the number of servers that can communicate with at least one other server (either in their row or column).
At first glance, you might think to check for each server whether there is another server in its row or column. This could be done by scanning the row and column for every server found, but that would result in a lot of repeated work, especially for large grids.
A brute-force approach would involve, for every server, scanning its row and column to see if there is another server. However, this would be inefficient, potentially taking O(N * M * (N + M)), where N and M are the number of rows and columns.
To optimize, notice that what matters for each cell is just how many servers are in its row and column. If a row or column has more than one server, then all servers in that row or column can communicate with at least one other server. So, we can first count the number of servers in each row and column, and then for each server, check if its row or column has more than one server.
To solve this efficiently, we use the following steps:
1
, increment the count for that row and column.This method avoids repeated scanning by leveraging the counts, making the solution much more efficient.
Let's walk through an example:
Input grid: [[1, 0], [1, 1]]
The optimized approach is much more efficient and can handle large grids within time limits.
The key insight is that servers can communicate if their row or column contains more than one server. By counting servers in each row and column, we can efficiently determine which servers can communicate, avoiding redundant checks. This leads to a solution with linear time complexity relative to the grid size, making it both elegant and practical for large inputs.