forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lonely-pixel-ii.cpp
35 lines (32 loc) · 1.02 KB
/
lonely-pixel-ii.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
// Time: O(m * n)
// Space: O(m * n)
class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int N) {
vector<int> rows = vector<int>(picture.size());
vector<int> cols = vector<int>(picture[0].size());
unordered_map<string, int> lookup;
vector<string> srows;
for (int i = 0; i < picture.size(); ++i) {
string s;
for (int j = 0; j < picture[0].size(); ++j) {
if (picture[i][j] == 'B') {
++rows[i];
++cols[j];
}
s.push_back(picture[i][j]);
}
++lookup[s];
srows.emplace_back(move(s));
}
int result = 0;
for (int i = 0; i < picture.size(); ++i) {
if (rows[i] == N && lookup[srows[i]] == N) {
for (int j = 0; j < picture[0].size(); ++j) {
result += picture[i][j] == 'B' && cols[j] == N;
}
}
}
return result;
}
};