-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.java
50 lines (49 loc) · 1.57 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
class Solution {
public int findBlackPixel(char[][] picture, int target) {
int m = picture.length, n = picture[0].length;
int[] rows = new int[m];
Map<Integer, List<Integer>> cols = new HashMap<>();
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
++rows[i];
cols.computeIfAbsent(j, k -> new ArrayList<>()).add(i);
}
}
}
boolean[][] t = new boolean[m][m];
for (int i = 0; i < m; ++i) {
for (int k = i; k < m; ++k) {
t[i][k] = i == k || all(picture[i], picture[k]);
t[k][i] = t[i][k];
}
}
int res = 0;
for (int i = 0; i < m; ++i) {
if (rows[i] == target) {
for (int j = 0; j < n; ++j) {
List<Integer> col = cols.get(j);
if (col != null && col.size() == target) {
boolean check = true;
for (int k : col) {
check = check && t[i][k];
}
if (check) {
++res;
}
}
}
}
}
return res;
}
private boolean all(char[] row1, char[] row2) {
int n = row1.length;
for (int j = 0; j < n; ++j) {
if (row1[j] != row2[j]) {
return false;
}
}
return true;
}
}