Smallest Rectangle Enclosing Black Pixels

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

Example

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]

and x = 0, y = 2,
Return 6.

java

public class Solution {
    /*
     * @param image: a binary matrix with '0' and '1'
     * @param x: the location of one of the black pixels
     * @param y: the location of one of the black pixels
     * @return: an integer
     */
    public int minArea(char[][] image, int x, int y) {
        // write your code here
        if (image == null || image.length == 0 ||
            image[0] == null || image[0].length == 0) {
            return 0;        
        }
        int left = findLeft(image, 0, y);
        int right = findRight(image, y, image[0].length - 1);
        int top = findTop(image, x, image.length - 1);
        int bottom = findBottom(image, 0, x);
        return (right - left + 1) * (top - bottom + 1);
    }
    private int findLeft(char[][] image, int start, int end) {
        int mid = 0;
        while (start + 1 < end) {
            mid = (end - start) / 2 + start;
            if (isPixel1(image, mid)) {
                end = mid;
            } else {
                start = mid;
            }
        }
        if (isPixel1(image, start)) {
            return start;
        } 
        return end;
    }
    
    private int findRight(char[][] image, int start, int end) {
        int mid = 0;
        while (start + 1 < end) {
            mid = (end - start) / 2 + start;
            if (isPixel1(image, mid)) {
                start = mid;
            } else {
                end = mid;
            }
        }
        if (isPixel1(image, end)) {
            return end;
        }
        return start;
    }
    
    private int findTop(char[][] image, int start, int end) {
        int mid = 0;
        while (start + 1 < end) {
            mid = (end - start) / 2 + start;
            if (isPixel2(image, mid)) {
                start = mid;
            } else {
                end = mid;
            }
        }
        if (isPixel2(image, end)) {
            return end;
        }
        return start;
    }
    
    private int findBottom(char[][] image, int start, int end) {
        int mid = 0;
        while (start + 1 < end) {
            mid = (end - start) / 2 + start;
            if (isPixel2(image, mid)) {
                end = mid;
            } else {
                start = mid;
            }
        }
        if (isPixel2(image, start)) {
            return start;
        }
        return end;
    }
    
    private boolean isPixel1(char[][] image, int column) {
        for (int i = 0; i < image.length; i++) {
            if (image[i][column] == '1') {
                return true;
            }
        }
        return false;
    }
    
    private boolean isPixel2(char[][] image, int row) {
        for (int i = 0; i < image[0].length; i++) {
            if (image[row][i] == '1') {
                return true;
            }
        }
        return false;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ncst

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值