You are given a map of a server center, represented as a m * n integer matrix grid, where 1 means that on that cell there is a server and 0 means that it is no server. Two servers are said to communicate if they are on the same row or on the same column.
Return the number of servers that communicate with any other server.
Example 1:
Input: grid = [
[1,1,0,0],
[0,0,1,0],
[0,0,1,0],
[0,0,0,1]
]
Output: 4Explanation: Except for the server at the position (3,3), all other servers have other servers to communicate.
Example 2:
Input: grid = [
[1,0],
[1,1]
]
Output: 3Explanation: All three servers can communicate with at least one other server.
Constraints:
1 <= grid.length, grid[i].length <= 250grid[i][j] == 0 or 1Before attempting this problem, you should be comfortable with:
A server can communicate if there is at least one other server in the same row or column. For each server, we can directly check its entire row and column to see if any other server exists. If we find at least one, this server counts toward our result.
1):class Solution:
def countServers(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
res = 0
for r in range(m):
for c in range(n):
if grid[r][c] == 0:
continue
found = False
for col in range(n):
if col != c and grid[r][col] == 1:
found = True
break
if not found:
for row in range(m):
if row != r and grid[row][c] == 1:
found = True
break
if found:
res += 1
return resWhere is the number of rows and is the number of columns of the given matrix .
Instead of checking each server's row and column individually, we can precompute the count of servers in each row and column. A server can communicate if there is more than one server in its row or more than one server in its column. This allows us to determine each server's status in constant time after the preprocessing step.
row_cnt to store server counts per row and col_cnt for columns.1.class Solution:
def countServers(self, grid: List[List[int]]) -> int:
ROWS, COLS = len(grid), len(grid[0])
row_cnt = [0] * ROWS
col_cnt = [0] * COLS
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] == 1:
row_cnt[r] += 1
col_cnt[c] += 1
res = 0
for r in range(ROWS):
for c in range(COLS):
if grid[r][c] and max(row_cnt[r], col_cnt[c]) > 1:
res += 1
return resWhere is the number of rows and is the number of columns of the given matrix .
We can avoid using extra arrays by processing rows and columns separately and using the grid itself to mark processed servers. First, we process all rows with multiple servers and mark those servers. Then, we process columns and only count unmarked servers in columns with multiple servers. The marking prevents double counting servers that belong to both a communicating row and column.
-1.-1 back to 1.class Solution:
def countServers(self, grid: List[List[int]]) -> int:
res = 0
ROWS, COLS = len(grid), len(grid[0])
# Rows
for r in range(ROWS):
row_sum = sum(grid[r])
if row_sum <= 1:
continue
res += row_sum
for c in range(COLS):
if grid[r][c]:
grid[r][c] = -1 # Mark
# Cols
for c in range(COLS):
col_sum = unmarked = 0
for r in range(ROWS):
col_sum += abs(grid[r][c])
if grid[r][c] > 0:
unmarked += 1
elif grid[r][c] < 0:
grid[r][c] = 1 # Unmark
if col_sum >= 2:
res += unmarked
return resWhere is the number of rows and is the number of columns of the given matrix .
A server only counts if it can communicate with at least one other server. Counting all servers (all cells with value 1) ignores the communication requirement.
A server communicates if its row has 2+ servers OR its column has 2+ servers. Using AND would incorrectly require both conditions.
# Wrong: requires both row AND column to have multiple servers
if row_cnt[r] > 1 and col_cnt[c] > 1:
# Correct: either condition is sufficient
if row_cnt[r] > 1 or col_cnt[c] > 1:When processing rows and columns separately (in the space-optimized solution), servers that are in both a communicating row and a communicating column can be counted twice. Mark processed servers to avoid this.