-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
533.cpp
63 lines (63 loc) · 2.22 KB
/
533.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
__________________________________________________________________________________________________
class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int N) {
if (picture.empty() || picture[0].empty()) return 0;
int m = picture.size(), n = picture[0].size(), res = 0, k = 0;
vector<int> rowCnt(m, 0), colCnt(n, 0);
vector<string> rows(m, "");
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rows[i].push_back(picture[i][j]);
if (picture[i][j] == 'B') {
++rowCnt[i];
++colCnt[j];
}
}
}
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (rowCnt[i] == N && colCnt[j] == N) {
for (k = 0; k < m; ++k) {
if (picture[k][j] == 'B') {
if (rows[i] != rows[k]) break;
}
}
if (k == m) {
res += colCnt[j];
colCnt[j] = 0;
}
}
}
}
return res;
}
};
__________________________________________________________________________________________________
class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int N) {
if (picture.empty() || picture[0].empty()) return 0;
int m = picture.size(), n = picture[0].size(), res = 0;
vector<int> colCnt(n, 0);
unordered_map<string, int> u;
for (int i = 0; i < m; ++i) {
int cnt = 0;
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
++colCnt[j];
++cnt;
}
}
if (cnt == N) ++u[string(picture[i].begin(), picture[i].end())];
}
for (auto a : u) {
if (a.second != N) continue;
for (int i = 0; i < n; ++i) {
res += (a.first[i] == 'B' && colCnt[i] == N) ? N : 0;
}
}
return res;
}
};
__________________________________________________________________________________________________