comments | difficulty | edit_url | tags | |||
---|---|---|---|---|---|---|
true |
Medium |
|
Given an m x n
picture
consisting of black 'B'
and white 'W'
pixels and an integer target, return the number of black lonely pixels.
A black lonely pixel is a character 'B'
that located at a specific position (r, c)
where:
- Row
r
and columnc
both contain exactlytarget
black pixels. - For all rows that have a black pixel at column
c
, they should be exactly the same as rowr
.
Example 1:
Input: picture = [["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","B","W","B","B","W"],["W","W","B","W","B","W"]], target = 3 Output: 6 Explanation: All the green 'B' are the black pixels we need (all 'B's at column 1 and 3). Take 'B' at row r = 0 and column c = 1 as an example: - Rule 1, row r = 0 and column c = 1 both have exactly target = 3 black pixels. - Rule 2, the rows have black pixel at column c = 1 are row 0, row 1 and row 2. They are exactly the same as row r = 0.
Example 2:
Input: picture = [["W","W","B"],["W","W","B"],["W","W","B"]], target = 1 Output: 0
Constraints:
m == picture.length
n == picture[i].length
1 <= m, n <= 200
picture[i][j]
is'W'
or'B'
.1 <= target <= min(m, n)
The second condition in the problem is equivalent to requiring that for each column containing black pixels, these rows are exactly the same.
Therefore, we can use an adjacency list
Next, we traverse each column. For each column, we find the first row
After the traversal, we return the answer.
The time complexity is
class Solution:
def findBlackPixel(self, picture: List[List[str]], target: int) -> int:
rows = [0] * len(picture)
g = defaultdict(list)
for i, row in enumerate(picture):
for j, x in enumerate(row):
if x == "B":
rows[i] += 1
g[j].append(i)
ans = 0
for j in g:
i1 = g[j][0]
if rows[i1] != target:
continue
if len(g[j]) == rows[i1] and all(picture[i2] == picture[i1] for i2 in g[j]):
ans += target
return ans
class Solution {
public int findBlackPixel(char[][] picture, int target) {
int m = picture.length;
int n = picture[0].length;
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
int[] rows = new int[m];
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
++rows[i];
g[j].add(i);
}
}
}
int ans = 0;
for (int j = 0; j < n; ++j) {
if (g[j].isEmpty() || (rows[g[j].get(0)] != target)) {
continue;
}
int i1 = g[j].get(0);
int ok = 0;
if (g[j].size() == rows[i1]) {
ok = target;
for (int i2 : g[j]) {
if (!Arrays.equals(picture[i1], picture[i2])) {
ok = 0;
break;
}
}
}
ans += ok;
}
return ans;
}
}
class Solution {
public:
int findBlackPixel(vector<vector<char>>& picture, int target) {
int m = picture.size();
int n = picture[0].size();
vector<int> g[n];
vector<int> rows(m);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (picture[i][j] == 'B') {
++rows[i];
g[j].push_back(i);
}
}
}
int ans = 0;
for (int j = 0; j < n; ++j) {
if (g[j].empty() || (rows[g[j][0]] != target)) {
continue;
}
int i1 = g[j][0];
int ok = 0;
if (g[j].size() == rows[i1]) {
ok = target;
for (int i2 : g[j]) {
if (picture[i1] != picture[i2]) {
ok = 0;
break;
}
}
}
ans += ok;
}
return ans;
}
};
func findBlackPixel(picture [][]byte, target int) (ans int) {
m := len(picture)
n := len(picture[0])
g := make([][]int, n)
rows := make([]int, m)
for i, row := range picture {
for j, x := range row {
if x == 'B' {
rows[i]++
g[j] = append(g[j], i)
}
}
}
for j := 0; j < n; j++ {
if len(g[j]) == 0 || rows[g[j][0]] != target {
continue
}
i1 := g[j][0]
ok := 0
if len(g[j]) == rows[i1] {
ok = target
for _, i2 := range g[j] {
if !bytes.Equal(picture[i1], picture[i2]) {
ok = 0
break
}
}
}
ans += ok
}
return
}
function findBlackPixel(picture: string[][], target: number): number {
const m: number = picture.length;
const n: number = picture[0].length;
const g: number[][] = Array.from({ length: n }, () => []);
const rows: number[] = Array(m).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (picture[i][j] === 'B') {
++rows[i];
g[j].push(i);
}
}
}
let ans: number = 0;
for (let j = 0; j < n; ++j) {
if (g[j].length === 0 || rows[g[j][0]] !== target) {
continue;
}
const i1: number = g[j][0];
let ok: number = 0;
if (g[j].length === rows[i1]) {
ok = target;
for (const i2 of g[j]) {
if (picture[i1].join('') !== picture[i2].join('')) {
ok = 0;
break;
}
}
}
ans += ok;
}
return ans;
}