Lonely Pixel I 两种算法之间的性能比较

今天参加LeetCode Weekly Contest 22,第二题 "Lonely Pixel I" 问题描述如下:


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

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

A black lonely pixel is character 'B' that located at a specific position where the same row and same column don't have any other black pixels.

Example

Input:
[['W', 'W', 'B'],
['W', 'B', 'W'],
['B', 'W', 'W']] Output: 3
Explanation: All the three 'B's are black lonely pixels.

Note:

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

我的解法 (Java)

根据 picture 的行数和列数生成两个数组 rowcount 和 colcount,并声明一个 inrowindex 数组,维数和rowcount的维数相同,用来记录 rowcount[i] 中的 'B' 所在的列数 j ;然后遍历 picture ,如果当前(i,j)位置处的Pixel为 'B' ,则进行 rowcount[i]++ 和 colcount[j]++操作, 并进行 inrowindex[i]=j 操作。

上述步骤操作完毕后,考察 rowcount[i]1 的那些行,如果rowcount[i]1,且 colcount[inrowindex[i]]==1,那么说明 picture[i][inrowindex[i]] 处的 'B' 为 Lonely Pixel。

算法实现如下:

public class Solution {
public int findLonelyPixel(char[][] picture) {
int rows=picture.length;
int cols=picture[0].length;
int[] colcount=new int[cols];
int[] rowcount=new int[rows];
int[] inrowindex=new int[rows];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
rowcount[i]++;
inrowindex[i]=j;//记录rows[i]处的'B'出现在第几列
colcount[j]++;
}
}
}
int count=0;
for(int i=0;i<rows;i++){
if(rowcount[i]==1){
if(colcount[inrowindex[i]]==1){
count++;
}
}
}
return count;
}
}

但是提交算法后,显示的结果是一个大的二维数组运算超时! 实在找不到更好的算法了。

晚上在网上看到了有人分享的算法,是用Python写的:

Python 算法

class Solution(object):
def findLonelyPixel(self, picture):
"""
:type picture: List[List[str]]
:rtype: int
"""
w, h = len(picture), len(picture[0])
rows, cols = [0] * w, [0] * h
for x in range(w):
for y in range(h):
if picture[x][y] == 'B':
rows[x] += 1
cols[y] += 1
ans = 0
for x in range(w):
for y in range(h):
if picture[x][y] == 'B':
if rows[x] == 1:
if cols[y] == 1:
ans += 1
return ans

感觉这个算法的思路和我的类似,而该算法第二步统计 Lonely Pixel 的个数的时候,又进行了一次二维数组遍历,还不如我的算法高效。但试着提交该算法,却 Accepted 了!

难道是Python的测试样例跟Java的测试样例不同导致的?

于是将该 Python 算法改写成 Java 算法,如下:

public class Solution {
public int findLonelyPixel(char[][] picture) {
int rows=picture.length;
int cols=picture[0].length;
int[] colcount=new int[cols];
int[] rowcount=new int[rows];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
rowcount[i]++;
colcount[j]++;
}
}
}
int count=0;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(picture[i][j]=='B'){
if(rowcount[i]==1&&colcount[j]==1){
count++;
}
}
}
}
return count;
}
}

竟然 Accepted了!

这是怎么回事呢?

第一个二层 for 循环,我仅仅多了一个 inrowindex[i]=j 操作,但这步操作正是为了第二个 for 循环只进行一层循环,为的是当 rowcount[i] 为 1 时,快速定位到这一行的 'B' 所在的列数,并考察这一列是否只有一个 'B'。 明明我的算法更高效呀!

这是怎么回事呢?

2017年3月5日20:00补充:

同样的算法,上午比赛的时候TLE,晚上又试了一次,这次竟然Accepted!! 什么鬼?

LeetCode 531----Lonely Pixel I----两种算法之间性能的比较的更多相关文章

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

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

  2. LeetCode 531. Lonely Pixel I

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

  3. [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 ...

  4. 图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS)

    参考网址:图文详解两种算法:深度优先遍历(DFS)和广度优先遍历(BFS) - 51CTO.COM 深度优先遍历(Depth First Search, 简称 DFS) 与广度优先遍历(Breath ...

  5. 最小生成树算法 prim kruskal两种算法实现 HDU-1863 畅通工程

    最小生成树 通俗解释:一个连通图,可将这个连通图删减任意条边,仍然保持连通图的状态并且所有边权值加起来的总和使其达到最小.这就是最小生成树 可以参考下图,便于理解 原来的图: 最小生成树(蓝色线): ...

  6. KingbaseES 两表关联Update的两种写法与性能

    熟悉oracle 的人都知道,对于两表的关联更新,其执行计划主要有 Filter 和 Outer Join 两种方式.对于大批量数据的update,Join方式明显是更优的选择.KingbaseES ...

  7. LeetCode 531. Longly Pixel I (孤独的像素之一) $

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

  8. 「每日五分钟,玩转JVM」:两种算法

    前言 上篇文章,我们了解了GC 的相关概念,这篇文章我们通过两个算法来了解如何去确定堆中的对象实例哪些是我们需要去回收的垃圾对象. 引用计数算法 引用计数法的原理很简单,就是在对象中维护一个计数器,当 ...

  9. 531. Lonely Pixel I

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

随机推荐

  1. JS实现表格列宽拖动

    在数据表格中,有时候需要拖动表格宽度,查看完整的数据,是很常用的功能. 1 效果 可以用纯JS就可以实现,如下,是正常情况下的表格: 拖动表格标题中间线,拖动后效果如下: 查看DEMO 2 代码 HT ...

  2. InfluxDB概念和基本操作 二

    InfluxDB概念和基本操作   InfluxDB基本概念 数据格式 在 InfluxDB 中,我们可以粗略的将要存入的一条数据看作一个虚拟的 key 和其对应的 value(field value ...

  3. 将python的代码文件打包成可执行文件

    1.使用pip install Pyinstaller  命令安装 2.使用命令 pyinstaller -F  *.py打包成exe 3.在\dist文件夹下找到exe; 一.pyinstaller ...

  4. hibernate调用mysql自己手动创建函数报错

    split为自己手动在mysql中创建的函数,在hibernate调用时出错,解决方案如下: jdbc调用可以.不用改hibernate的方言. 正常的为:

  5. Django分页的实现

    Django分页的实现 Django ORM  分页介绍 分页是网页浏览中常见到的一种形式,在数据量较大时,一个页面显示不全,采取分割数据由用户选择进行显示的方式. 基本实现 技术点 通过切片得到数据 ...

  6. [BZOJ 5323][Jxoi2018]游戏

    传送门 \(\color{green}{solution}\) 它每次感染的人是它的倍数,那么我们只需要找出那些除了自己以外在\(l\), \(r\)内没有别的数是 它的约数的数,在这里称其为关键数. ...

  7. 【算法笔记】A1060 Are They Equal

    1060 Are They Equal (25 分)   If a machine can save only 3 significant digits, the float numbers 1230 ...

  8. cool kickass

    I can stay like this alllllllllll daaaaaaaaayyyyyy.

  9. stark - 5 ⇲ 其他常用功能

    Ⅰ 排序 当数据量增多,对于数据 我们应该能够指定如何排序的.且此功能应该是可以给用户自定义进行配置的. 这是StarkHandler类的方法1 order_list = [] def get_ord ...

  10. OpenERP中自定义模块卸载失败,Postgres数据库删不掉数据库,OpenERP登录不了一直在加载的问题解决方案。

    解决方案也就是删除掉不用的数据库,OE会提示当前有N个Session不让Drop数据库. 对于Postgres 9.1 版本,在pgAdmin中查询以下语句: SELECT pg_terminate_ ...