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. QuantLib 金融计算——基本组件之 Index 类

    目录 QuantLib 金融计算--基本组件之 Index 类 QuantLib 金融计算--基本组件之 Index 类 Index 类用于表示已知的指数或者收益率,例如 Libor 或 Shibor ...

  2. 2. 需要对测试用的数据进行MD5加密

    import hashlib phone_num = open("D:/testdata/phone10.txt","r") out_file = open(& ...

  3. FCN详解

    转载自:https://www.cnblogs.com/gujianhan/p/6030639.html 论文地址:https://arxiv.org/pdf/1411.4038v1.pdf 背景 C ...

  4. word前页与后页页码断开

    方法一:以Word2013为例:1. 光标移动到目录页的最后一行,从“页面布局”选项卡“分隔符”中选择“下一页”类型的“分节符”,删除多余的行.分页符等(图1): 2. 双击正文任意一页的页眉/页脚区 ...

  5. Windows7上用VS编译本地使用的live555

    本文链接:https://www.jianshu.com/p/6ea100865744 环境 系统:Windows7 SP1 64位 编辑器:Visual Studio Community 2017 ...

  6. (转)DB2高可用性灾难恢复

    DB2 HADR概述 原文:https://www.ibm.com/developerworks/cn/data/library/techarticles/dm-0508luojunkai/

  7. Java之重载(Overload)与重写(Overwrite)总结

    内容来源为:<孙卫琴面向对象编程>,本随笔简单总结,具体内容可参见概述第6章,写的挺清晰: 一. 重载(Overload) 1. 有时候类的同一种功能有多种实现方式,到底采用哪种实现方式, ...

  8. 高性能队列Disruptor的使用

    一.什么是 Disruptor 从功能上来看,Disruptor 是实现了"队列"的功能,而且是一个有界队列.那么它的应用场景自然就是"生产者-消费者"模型的应 ...

  9. Linux下Tomcat8.0.44配置使用Apr

    听说Apr可以提高tomcat很多的性能,配置具体如下 1.安装apr 1.5.2 [root@ecs-3c46 ]# cd /usr/local/src [root@ecs-3c46 src]# w ...

  10. WPF中Label使用StringFormat

    1. 在WPF中Label的Content有时内容只需要改变个别数字,而不需要所以内容都修改,这时候就要使用StringFormat, 如: <Label Content="I hav ...