-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsolution.java
29 lines (27 loc) · 847 Bytes
/
solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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];
// First pass: Count servers in each row and column
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1) {
rowCount[i]++;
colCount[j]++;
}
}
}
// Second pass: Count communicable servers
int count = 0;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (grid[i][j] == 1 && (rowCount[i] > 1 || colCount[j] > 1)) {
count++;
}
}
}
return count;
}
}