-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
LonelyPixelI.java
36 lines (34 loc) · 1.09 KB
/
LonelyPixelI.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
package problems.medium;
import java.util.HashMap;
import java.util.Map;
/**
* Created by sherxon on 3/5/17.
*/
public class LonelyPixelI {
public int findLonelyPixel(char[][] a) {
Map<Integer, Integer> rows = new HashMap<>();
Map<Integer, Integer> cols = new HashMap<>();
int count = 0;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] == 'B') {
if (!rows.containsKey(i) && !cols.containsKey(j)) {
count++;
cols.put(j, 1);
rows.put(i, 1);
} else {
if (cols.getOrDefault(j, -1) == 1) {
count--;
cols.put(j, 2);
}
if (rows.getOrDefault(i, -1) == 1) {
count--;
rows.put(i, 2);
}
}
}
}
}
return count < 0 ? 0 : count;
}
}