Description#
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:
![](https://spcdn.pages.dev/leetcode/problems/1267.Count%20Servers%20that%20Communicate/images/untitled-diagram-6.jpg)
Input: grid = [[1,0],[0,1]]
Output: 0
Explanation: No servers can communicate with others.
Example 2:
![](https://spcdn.pages.dev/leetcode/problems/1267.Count%20Servers%20that%20Communicate/images/untitled-diagram-4.jpg)
Input: grid = [[1,0],[1,1]]
Output: 3
Explanation: All three servers can communicate with at least one other server.
Example 3:
![](https://spcdn.pages.dev/leetcode/problems/1267.Count%20Servers%20that%20Communicate/images/untitled-diagram-1-3.jpg)
Input: grid = [[1,1,0,0],[0,0,1,0],[0,0,1,0],[0,0,0,1]]
Output: 4
Explanation: The two servers in the first row can communicate with each other. The two servers in the third column can communicate with each other. The server at right bottom corner can't communicate with any other server.
Constraints:
m == grid.length
n == grid[i].length
1 <= m <= 250
1 <= n <= 250
grid[i][j] == 0 or 1
Solutions#
Solution 1: Counting#
We can count the number of servers in each row and each column, then traverse each server. If the number of servers in the current server’s row or column exceeds $1$, it means the current server meets the condition, and we increment the result by $1$.
After the traversal, we return the result.
The time complexity is $O(m \times n)$, and the space complexity is $O(m + n)$. Where $m$ and $n$ are the number of rows and columns in the matrix, respectively.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| class Solution:
def countServers(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
row = [0] * m
col = [0] * n
for i in range(m):
for j in range(n):
if grid[i][j]:
row[i] += 1
col[j] += 1
return sum(
grid[i][j] and (row[i] > 1 or col[j] > 1)
for i in range(m)
for j in range(n)
)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| class Solution {
public int countServers(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] row = new int[m];
int[] col = new int[n];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
row[i]++;
col[j]++;
}
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == 1 && (row[i] > 1 || col[j] > 1)) {
++ans;
}
}
}
return ans;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| class Solution {
public:
int countServers(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> row(m), col(n);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j]) {
++row[i];
++col[j];
}
}
}
int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
ans += grid[i][j] && (row[i] > 1 || col[j] > 1);
}
}
return ans;
}
};
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| func countServers(grid [][]int) (ans int) {
m, n := len(grid), len(grid[0])
row, col := make([]int, m), make([]int, n)
for i := range grid {
for j, x := range grid[i] {
if x == 1 {
row[i]++
col[j]++
}
}
}
for i := range grid {
for j, x := range grid[i] {
if x == 1 && (row[i] > 1 || col[j] > 1) {
ans++
}
}
}
return
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
| function countServers(grid: number[][]): number {
const m = grid.length;
const n = grid[0].length;
const row = new Array(m).fill(0);
const col = new Array(n).fill(0);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
row[i]++;
col[j]++;
}
}
}
let ans = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1 && (row[i] > 1 || col[j] > 1)) {
ans++;
}
}
}
return ans;
}
|