LeetCode 533. Lonely Pixel II (孤独的像素之二) $

时间:2021-07-19 06:55:43

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].

题目标签:Array

  题目给了我们一个2D array picture 和一个N,让我们从picture 中找到 符合 两条规则的black pixel的个数。

  rule 2 真是一开始没理解,琢磨了一会,去看了解释才明白。

  rule 2 说的是,当满足了rule 1 有一个在row R 和 column C 的black pixel,这个像素的行 和列 都要有N个 black pixels之后,

          还需要满足column C 中 有black pixels 的 rows 都要和 R 这一行row 一摸一样。

  建立一个Map,把row String(把每一行char组成string) 当作key, 把这一个row 出现过的次数 当作value;还需要建立一个cols array,来记录每一列的black pixels个数。

  遍历picture:

    1. 记录每一列的B 个数;

    2. 记录每一行的B 个数,只有等于N的情况下,才把row string 存入map。(每一行的black pixels个数在这里就被完成了,所以不需要额外空间来存放)

  遍历map 的keySet(有N个B的行):

    1. 如果这个row出现的次数 不等于 N的话, 说明 不满足rule 1的一列里要有N个B的条件。 因为如果当前 row 出现了N次,而且row 在之前已经满足了一行里有N个B的条件。所以每行里肯定有B,然后有B的一列里也会有N个B。

        当满足了row出现的次数 等于N 之后,意味着也满足了rule 2,因为这N个rows 都是一摸一样的。

    2. 当满足了上面这个条件后,遍历所有列:把每列的B的个数N加入res。

Java Solution:

Runtime beats 71.04%

完成日期:09/25/2017

关键词:Array, HashMap

关键点:建立一个HashMap,使得每一行的string 和 它的出现次数形成映射(满足rule1 rule2);建立array cols 来辅助找到每一列中符合标准的B的数量

 class Solution
{
public int findBlackPixel(char[][] picture, int N)
{
int m = picture.length;
int n = picture[0].length;
int[] cols = new int[n];
HashMap<String, Integer> map = new HashMap<>();
int res = 0; // iterate picture
for(int i=0; i<m; i++) // rows
{
int count = 0;
StringBuilder sb = new StringBuilder(); for(int j=0; j<n; j++) // cols
{
if(picture[i][j] == 'B')
{
cols[j]++;
count++;
}
sb.append(picture[i][j]);
} if(count == N) // only store rowString into map when this row has N B
{
String curRow = sb.toString();
map.put(curRow, map.getOrDefault(curRow, 0) + 1); // count how many rows are same
} } // iterate keySet
for(String row : map.keySet())
{
if(map.get(row) != N) // if value is not N, meaning rule 1 (columns need to have N Bs) is not satisfied.
continue;
// if value is N, meaning rule 2 is also satisfied because all these rows are same.
for(int j=0; j<n; j++) // iterate column
{ // count Bs in this column
if(row.charAt(j) == 'B' && cols[j] == N)
res += N;
}
} return res;
}
}

参考资料:

https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap

LeetCode 题目列表 - LeetCode Questions List