forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_1267.java
33 lines (32 loc) · 1.06 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
package com.fishercoder.solutions;
public class _1267 {
public static class Solution1 {
/**
* credit: https://leetcode.com/problems/count-servers-that-communicate/discuss/436188/Java-or-Clean-And-Simple-or-Beats-100
*/
public int countServers(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
int[] rowCount = new int[m];
int[] columnCount = new int[n];
int total = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
rowCount[i]++;
columnCount[j]++;
total++;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 && rowCount[i] == 1 && columnCount[j] == 1) {
total--;
}
}
}
return total;
}
}
}