zoukankan      html  css  js  c++  java
  • LeetCode 531----Lonely Pixel I----两种算法之间性能的比较

    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!! 什么鬼?

  • 相关阅读:
    科学开源项目 : 建立一个 新的 光衍射 模型
    光不是电磁波 摩擦力不是电磁力
    收录一篇 贝尔不等式 的 文章
    数学家 程序员 哲学家 艺术家
    论 数学 的 工具性
    数学 怎么用?
    随便说说 广义相对论 的 时间膨胀
    逻辑物理学 : 光子 有 质量 吗 ?
    我对 “光子有质量 , 引力对光子有作用” 存疑
    收录一些 高等数学 的 文章
  • 原文地址:https://www.cnblogs.com/dongling/p/6506488.html
Copyright © 2011-2022 走看看