Thursday, March 9, 2017

Smallest Rectangle Enclosing Black Pixels

This is an interview question of Google.
My very first idea of solving this problem is using dfs algorithm.
However, this question is under the binary search category, so... I have no idea!
A great blog article about this question: http://www.cnblogs.com/grandyang/p/5268775.html

My answer:
Version #1Not work for big dataset
这个版本就是无脑dfs,一开始的整体框架写的非常之顺,自己手动testcase也没有什么大问题。然而跑的时候结果一直很诡异,无奈print了一下最后的返回边界,发现xmin, xmax...这四个值完全没有变动过。
这里的问题是把java基本语法忘光了!!!生气!就是我把四个int作为argument传进了method里面,妄图在method里面改它们,然而java是pass by value的,所以根本不能改
--> 挣扎1
既然primitive type不能改,那我传Integer总行了吧?于是大概改成了这样:

结果还是岿然不动。我想了想这么传的话无异于把一个新的Integer赋值给了原来的reference啊,怪不得!所以妄想着java里面有一个set之类的函数可以只改变Integer的value
--> 挣扎2
迅速扫了一眼stackoverflow里面似乎提到 number.set(*)这样的函数。于是胡乱写了一下,结果当然是不能compile。后来查到Integer是immutable的,create之后就没法改。但是有其他可以表示integer的type可以更改值。
--> 挣扎3
最后实在不行只能把这四个参数作为global variable了,因为它们本来就是全局的极值。这个方案一开始有想到,但是不太想用global variable就没用,权衡之后还是改回来了。最终就是如下代码。
--> 挣扎4 game over
果不其然!因为是无脑dfs所以在跑到87%的testcase时候遇到了big data set就华丽地Stack Overflow了,这个也是一开始就该想到的。所以此处觉得应该用stack代替无脑递归的方法。不过如果Python的话就不存在这个问题了。stack替代法还不会=。=于是game over
总而言之这个方案是可行的,只是出现了对java语法不熟悉导致的bug。

public class Solution {
    /**
     * @param image a binary matrix with '0' and '1'
     * @param x, y the location of one of the black pixels
     * @return an integer
     */
    int[] dx = {1, 0, -1, 0};
    int[] dy = {0, 1, 0, -1};
    Integer xmin = Integer.MAX_VALUE;
    Integer ymin = Integer.MAX_VALUE;
    Integer xmax = Integer.MIN_VALUE;
    Integer ymax = Integer.MIN_VALUE;
    public int minArea(char[][] image, int x, int y) {
        // Write your code here
        if (image == null || image.length == 0 || image[0].length == 0) {
            return 0;
        }
     
        //System.out.println("hi" + " x: " + x + " y: " + y);
        dfs(image, x, y);
        //System.out.println("hello");
        //System.out.println(xmin + " " + xmax + " " + ymin + " " + ymax);
        return (xmax - xmin + 1) * (ymax - ymin + 1);
    }
 
    private void dfs(char[][] image, int px, int py) {
        //System.out.println("here");
        xmin = Math.min(xmin, px);
        xmax = Math.max(xmax, px);
        ymin = Math.min(ymin, py);
        ymax = Math.max(ymax, py);
        //System.out.println(xmin + " " + xmax + " " + ymin + " " + ymax);
        //System.out.println();
        //to mark it as visited in case of being visited twice
        image[px][py] = '2';
        for (int i = 0; i < 4; i++) {
            if (isBlack(image, px + dx[i], py + dy[i])) {
                //System.out.println((px + dx[i]) + " " + (py + dy[i]) + "isblack");
                //System.out.println(xmin + " " + xmax + " " + ymin + " " + ymax);
                dfs(image, px + dx[i], py + dy[i]);
            }
        }
     
    }
    private boolean isBlack(char[][] image, int px, int py) {
        if (px < 0 || px >= image.length) {
            return false;
        }
        if (py < 0 || py >= image[0].length) {
            return false;
        }
        return image[px][py] == '1';
    }
}

No comments:

Post a Comment