# LeetCode: 533. 孤独像素II¶

## 1、题目描述¶

1. R 和列C都恰好包括N个黑色像素。
2. C中所有黑色像素所在的行必须和行R完全相同。

[['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'B', 'W', 'B', 'B', 'W'],
['W', 'W', 'B', 'W', 'B', 'W']]

N = 3

0    1    2    3    4    5         列号
0    [['W', 'B', 'W', 'B', 'B', 'W'],
1     ['W', 'B', 'W', 'B', 'B', 'W'],
2     ['W', 'B', 'W', 'B', 'B', 'W'],
3     ['W', 'W', 'B', 'W', 'B', 'W']]

• 输入二维数组行和列的范围是 [1,200]

## 2、解题思路¶

• 首先统计出列中的B数量等于N并且B对应的行完全一致的列，进行标记，同时统计出每一行的B的数量
• 然后在满足条件的列中查找，是否存在恰好B的数量等于N并且该行等于此列中B元素对应的行
class Solution:
def findBlackPixel(self, picture: List[List[str]], N: int) -> int:
row, col = len(picture), len(picture[0])
row_count = [0] * row
col_count = [0] * col
col_pos = [-1] * col
ans = 0

for j in range(col):
current = 0
pre = []
for i in range(row):
if picture[i][j] == "B":
if col_pos[j] == -1:
col_pos[j] = i
current += 1
row_count[i] += 1
if not pre:
pre = picture[i]
if current >= 0:
if picture[i] != pre:
current = -500
if current == N:
col_count[j] = N

for j in range(col):
if col_count[j] == N:
for i in range(row):
if row_count[i] == N and picture[i] == picture[col_pos[j]]:
ans += N
break
return ans