Skip to content

Commit 6aea026

Browse files
authored
Create Count Servers That Communicate.java
1 parent d9b3f38 commit 6aea026

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int countServers(int[][] grid) {
3+
if (grid.length == 0 || grid[0].length == 0) {
4+
return 0;
5+
}
6+
int numRows = grid.length;
7+
int numCols = grid[0].length;
8+
int[] rowCount = new int[numRows];
9+
int[] colCount = new int[numCols];
10+
int numOfServers = 0;
11+
for (int i = 0; i < numRows; i++) {
12+
for (int j = 0; j < numCols; j++) {
13+
if (grid[i][j] == 1) {
14+
numOfServers++;
15+
rowCount[i]++;
16+
colCount[j]++;
17+
}
18+
}
19+
}
20+
for (int i = 0; i < numRows; i++) {
21+
for (int j = 0; j < numCols; j++) {
22+
if (grid[i][j] == 1) {
23+
if (rowCount[i] == 1 && colCount[j] == 1) {
24+
numOfServers--;
25+
}
26+
}
27+
}
28+
}
29+
return numOfServers;
30+
}
31+
}

0 commit comments

Comments
 (0)