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

LeetCode 533. Lonely Pixel II (孤独的像素之二) $的更多相关文章

  1. [LeetCode] 533. Lonely Pixel II 孤独的像素 II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  2. [LeetCode] Lonely Pixel II 孤独的像素之二

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  3. [LeetCode] 531. Lonely Pixel I 孤独的像素 I

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  4. [LeetCode] Lonely Pixel I 孤独的像素之一

    Given a picture consisting of black and white pixels, find the number of black lonely pixels. The pi ...

  5. 533. Lonely Pixel II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...

  6. LeetCode 531. Lonely Pixel I

    原题链接在这里:https://leetcode.com/problems/lonely-pixel-i/ 题目: Given a picture consisting of black and wh ...

  7. [LeetCode] Number of Islands II 岛屿的数量之二

    A 2d grid map of m rows and n columns is initially filled with water. We may perform an addLand oper ...

  8. [LeetCode] 685. Redundant Connection II 冗余的连接之二

    In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...

  9. [LeetCode] Shortest Word Distance II 最短单词距离之二

    This is a follow up of Shortest Word Distance. The only difference is now you are given the list of ...

随机推荐

  1. linux下Nginx配置文件(nginx.conf)配置设置详解(windows用phpstudy集成)

    linux备份nginx.conf文件举例: cp /usr/local/nginx/nginx.conf /usr/local/nginx/nginx.conf-20171111(日期) 在进程列表 ...

  2. [js高手之路]Node.js+jade+express+mongodb+mongoose+promise实现todolist

    promise主要是用来解决异步回调问题,其实还有好几种比promise更好的方案,后面再说,这节,我们先用promise来改造下,我以前写的一篇文章[js高手之路]javascript腾讯面试题学习 ...

  3. JSR303的数据校验-Hibernate Validator方式实现

    1.什么是JSR303? JSR303是java为bean数据合法性校验所提供的一个标准规范,叫做Bean Validation. Bean Validation是一个运行时的数据校验框架,在验证之后 ...

  4. 【Python学习笔记之一】Python关键字及其总结

    前言 最近在学习Java Sockst的时候遇到了一些麻烦事,我觉得我很有必要重新研究学习Python这种脚本语言,参考大神的经验,淘到了一本学习Python的好书<"笨方法" ...

  5. Apache Spark 2.2.0 中文文档 - 集群模式概述 | ApacheCN

    集群模式概述 该文档给出了 Spark 如何在集群上运行.使之更容易来理解所涉及到的组件的简短概述.通过阅读 应用提交指南 来学习关于在集群上启动应用. 组件 Spark 应用在集群上作为独立的进程组 ...

  6. program 1 : python codes for login program(登录程序python代码)

    #improt time module for count down puase time import time #set var for loop counting counter=1 #logi ...

  7. centos7.2 linux 64位系统上安装mysql

    1.在线安装mysql 在终端中命令行下输入(在官网下载mysql): # wget https://dev.mysql.com/downloads/repo/yum/mysql57-communit ...

  8. 计算机基础--Java中int char byte的关系

    计算机基础--Java中int char byte的关系 重要:一个汉字占用2byte,Java中用char(0-65535 Unicode16)型字符来存字(直接打印输出的话是字而非数字),当然要用 ...

  9. Json操作问题总结

    大家都知道,Json是一种轻量级的数据交换格式,对JS处理数据来说是很理想滴! 熟练写过xxx.json文件和操作的小伙伴来说,我说的问题都不是什么大问题啦,可以忽略本宝宝的文章,更希望各位大佬指点一 ...

  10. 1523. K-inversions URAL 求k逆序对,,,,DP加树状数组

    1523. K-inversions Time limit: 1.0 secondMemory limit: 64 MB Consider a permutation a1, a2, …, an (a ...