LeetCode in Python 533. Lonely Pixel II

本文介绍了一种算法,用于在黑白像素构成的图片中找出符合特定条件的黑色像素:所在行和列各包含N个黑色像素,且所有含有该列黑色像素的行必须与目标行完全相同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a picture consisting of black and white pixels, and a positive integer N, find the number of black pixels located at some specific row R and column C that align with all the following rules:

  1. Row R and column C both contain exactly N black pixels.
  2. For all rows that have a black pixel at column C, they should be exactly the same as row R

The picture is represented by a 2D char array consisting of 'B' and 'W', which means black and white pixels respectively.

Example:

Input:                                            
[['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
Output: 6
Explanation: All the bold 'B' are the black pixels we need (all 'B's at column 1 and 3).
        0    1    2    3    4    5         column index                                            
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']]    
row index

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 N = 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.

Note:

    1. The range of width and height of the input 2D array is [1,200].

Solution: (未测试)

def lonelyPixel2(self, grid):
    rows, cols = len(grid), len(grid[0])
    rowCountMap = colCount = [0 for i in range(cols)]
    for i in range(rows):
        rowCount = 0
        for j in range(cols):
            if grid[i][j] == 'W': continue
            rowCount += 1
            colCount[j] += 1
        rowCountMap[rowCount] += 1

    count = 0
    for blackCount in colCount:
        count += rowCountMap[count]
    return count

用两个list代替map,rowCountMap[n]表示具有n个'B'格子的行的数量,colCount[i]表示第i列具有的'B'格子数量。n^2遍历一次可初始化这两个list,再根据colCount比较map,如果value不为0,说明找到了解,累加个数即可。

转载于:https://www.cnblogs.com/lowkeysingsing/p/11196232.html

### LeetCode533Python 解决方案 LeetCode533题名为 **Lonely Pixel II**,其核心问题是给定一个由 `'B'` 和 `'W'` 构成的二维矩阵 `picture`,找到满足特定条件的黑色像素数量。具体来说,这些黑色像素所在的行和列中恰好有 `N` 个黑色像素。 以下是该问题的一个可能解决方案: #### 方法描述 通过两次遍历实现: 1. 首先统计每一行中的黑色像素数目。 2. 然后检查每列是否有符合条件的黑色像素,并验证它们所在行是否也符合要求。 下面是具体的代码实现[^6]: ```python class Solution: def findBlackPixel(self, picture: List[List[str]], N: int) -> int: m, n = len(picture), len(picture[0]) row_counts = [0] * m col_counts = [0] * n # 记录每一行的字符串模式 row_patterns = [] for i in range(m): pattern = ''.join(picture[i]) row_patterns.append(pattern) row_counts[i] = sum(1 for c in picture[i] if c == 'B') for j in range(n): col_counts[j] = sum(1 for i in range(m) if picture[i][j] == 'B') result = 0 for j in range(n): if col_counts[j] != N: continue seen_rows = {} for i in range(m): if picture[i][j] == 'B': if row_counts[i] != N: break pattern = row_patterns[i] if pattern not in seen_rows: seen_rows[pattern] = 0 seen_rows[pattern] += 1 valid = all(count == N for count in seen_rows.values()) if valid and any(seen_rows.get(row_patterns[k], 0) > 0 for k in range(m) if picture[k][j] == 'B'): result += N return result ``` 此算法的时间复杂度主要取决于输入大小 \(O(M \times N)\),其中 \(M\) 是行数,\(N\) 是列数。空间复杂度则为 \(O(M + N)\)[^6]。 #### 关键点解析 - 使用辅助数组记录行列中的黑格子计数以便快速查询。 - 利用哈希表存储相同行模式的数量来判断是否存在多个相同的行匹配目标条件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值