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. eNSP 模拟器添加loopback本地回环口

    eNSP只能模拟华为的设备,通常情况下数据通信的传递范围仅限于eNSP中的设备之间. 有时我们在学习更多技术,比如希望将eNSP跟VMware Workstation里的虚拟机互通,或者想让eNSP里 ...

  2. (转)zabbix之生产案例

    原文: https://www.abcdocker.com/abcdocker/category/zabbix/ 原文: https://chegva.com/1170.html

  3. WCF系列教程之WCF服务协定

    本文参考自:http://www.cnblogs.com/wangweimutou/p/4422883.html,纯属读书笔记,加深记忆 一.服务协定简介: 1.WCF所有的服务协定层里面的服务接口, ...

  4. 微服务Kong(五)——添加一个用户(Consumer)

    在本节中,我们将学习如何添加一个用户(consumer)到KONG实例中.用户是与使用您的API的个人相关联,可用于跟踪,访问管理等. NOTE:本节假设您已经正确启用了密钥验证插件.如果没有,请参考 ...

  5. linux centos 7.5 安装mysql5.7

    1 下载tar包,这里使用wget从官网下载(注:下载地址随时可能有变动,wget命令默认下载目录为当前所在文件夹) wget https://dev.mysql.com/get/Downloads/ ...

  6. ASP.NET Core 的启动和运行机制

    目录 ASP .NET Core 的运行机制 ASP .NET Core 的启动 ASP .NET Core 的管道和中间件 参考 ASP .NET Core 的运行机制 Web Server: AS ...

  7. nodejs学习笔记二(get请求、post请求、 querystring模块,url模块)

    请求数据 前台:form.ajax.jsonp 后台:接受请求并返回响应数据     前台<= http协议 =>后台   常用的请求的方式: 1.GET           数据在url ...

  8. EF那点事

    EntityFramework 1-->什么是EnitityFramework   1.1--> ADO.NET Entity Framework 是微软以 ADO.NET 为基础所发展出 ...

  9. [转]Web API OData V4 Keys, Composite Keys and Functions Part 11

    本文转自:https://damienbod.com/2014/09/12/web-api-odata-v4-keys-composite-keys-and-functions-part-11/ We ...

  10. JavaScript ES6 promiss的理解。

    本着互联网的分享精神,我将我对promise的理解分享给大家. JavaScript ES6的promise方法主要应用在处理异步函数返回的结果,注意他不是将异步函数转换为同步函数,而是等异步函数有结 ...