LeetCode 533. Lonely Pixel II (孤独的像素之二) $
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:
- Row R and column C both contain exactly N black pixels.
- 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:
- 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的数量
1 class Solution 2 { 3 public int findBlackPixel(char[][] picture, int N) 4 { 5 int m = picture.length; 6 int n = picture[0].length; 7 int[] cols = new int[n]; 8 HashMap<String, Integer> map = new HashMap<>(); 9 int res = 0; 10 11 // iterate picture 12 for(int i=0; i<m; i++) // rows 13 { 14 int count = 0; 15 StringBuilder sb = new StringBuilder(); 16 17 for(int j=0; j<n; j++) // cols 18 { 19 if(picture[i][j] == 'B') 20 { 21 cols[j]++; 22 count++; 23 } 24 sb.append(picture[i][j]); 25 } 26 27 if(count == N) // only store rowString into map when this row has N B 28 { 29 String curRow = sb.toString(); 30 map.put(curRow, map.getOrDefault(curRow, 0) + 1); // count how many rows are same 31 } 32 33 } 34 35 // iterate keySet 36 for(String row : map.keySet()) 37 { 38 if(map.get(row) != N) // if value is not N, meaning rule 1 (columns need to have N Bs) is not satisfied. 39 continue; 40 // if value is N, meaning rule 2 is also satisfied because all these rows are same. 41 for(int j=0; j<n; j++) // iterate column 42 { // count Bs in this column 43 if(row.charAt(j) == 'B' && cols[j] == N) 44 res += N; 45 } 46 } 47 48 return res; 49 } 50 }
参考资料:
https://discuss.leetcode.com/topic/81686/verbose-java-o-m-n-solution-hashmap
LeetCode 题目列表 - LeetCode Questions List