- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1267.java
33 lines (32 loc) · 1.07 KB
/
_1267.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
30
31
32
33
packagecom.fishercoder.solutions.secondthousand;
publicclass_1267 {
publicstaticclassSolution1 {
/*
* credit: https://leetcode.com/problems/count-servers-that-communicate/discuss/436188/Java-or-Clean-And-Simple-or-Beats-100
*/
publicintcountServers(int[][] grid) {
intm = grid.length;
intn = grid[0].length;
int[] rowCount = newint[m];
int[] columnCount = newint[n];
inttotal = 0;
for (inti = 0; i < m; i++) {
for (intj = 0; j < n; j++) {
if (grid[i][j] == 1) {
rowCount[i]++;
columnCount[j]++;
total++;
}
}
}
for (inti = 0; i < m; i++) {
for (intj = 0; j < n; j++) {
if (grid[i][j] == 1 && rowCount[i] == 1 && columnCount[j] == 1) {
total--;
}
}
}
returntotal;
}
}
}