LeetCode 302. Smallest Rectangle Enclosing Black Pixels(最小面积矩形)

题目来源于LeetCode 302,要求找到包含所有黑色像素的最小轴对齐矩形的面积。图像由黑白像素表示,黑色像素连通。给定一个黑色像素的位置,需求出包围所有黑色像素的矩形面积。例如,给定特定图像,返回的矩形面积为10。

原题网址:https://leetcode.com/problems/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.

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]
and  x = 0 y = 2 ,

Return 6.

方法一:深度优先(递归)搜索,类似填色算法,时间复杂度O(nm)

public class Solution {
    private int left, right, top, bottom;
    private void scan(char[][] image, int x, int y) {
        if (x < 0 || x >= image.length || y < 0 || y >= image[x].length || image[x][y] == '0') return;
        left = Math.min(left, y);
        right = Math.max(right, y);
        top = Math.min(top, x);
        bottom = Math.max(bottom, x);
        image[x][y] = '0';
        scan(image, x, y-1);
        scan(image, x, y+1);
        scan(image, x-1, y);
        scan(image, x+1, y);
    }
    public int minArea(char[][] image, int x, int y) {
        left = y;
        right = y;
        top = x;
        bottom = x;
        scan(image, x, y);
        return (right-left+1)*(bottom-top+1);
    }
}

方法二:二分法搜索,即分别对(x,y)的上下左右方向应用二分法查找,是二分法一个奇葩的应用。

public class Solution {
    public int minArea(char[][] image, int x, int y) {
        int left = y, right = y, top = x, bottom = x;
        int low, high;
        low = 0;
        high = y;
        while (low<=high) {
            boolean black = false;
            int m = (low+high)/2;
            for(int i=0; i<image.length; i++) {
                if (image[i][m]=='1') {
                    black = true;
                    break;
                }
            }
            if (black) high = m - 1; else low = m + 1;
        }
        left = low;
        
        low = y;
        high = image[x].length-1;
        while (low<=high) {
            boolean black = false;
            int m = (low+high)/2;
            for(int i=0; i<image.length; i++) {
                if (image[i][m]=='1') {
                    black = true;
                    break;
                }
            }
            if (black) low = m + 1; else high = m - 1;
        }
        right = low;
        
        low = 0;
        high = x;
        while (low<=high) {
            boolean black = false;
            int m = (low+high)/2;
            for(int i=0; i<image[m].length; i++) {
                if (image[m][i] == '1') {
                    black = true;
                    break;
                }
            }
            if (black) high = m - 1; else low = m + 1;
        }
        top = low;
        
        low = x;
        high = image.length-1;
        while (low<=high) {
            boolean black = false;
            int m = (low+high)/2;
            for(int i=0; i<image[m].length; i++) {
                if (image[m][i] == '1') {
                    black = true;
                    break;
                }
            }
            if (black) low = m + 1; else high = m - 1;
        }
        bottom = low;
        
        return (right-left)*(bottom-top);
    }
}

简化:

public class Solution {
    private boolean column(char[][] image, int col) {
        for(int i=0; i<image.length; i++) {
            if (image[i][col] == '1') return true;
        }
        return false;
    }
    private boolean row(char[][] image, int row) {
        for(int i=0; i<image[row].length; i++) {
            if (image[row][i] == '1') return true;
        }
        return false;
    }
    public int minArea(char[][] image, int x, int y) {
        
        
        int left, right, top, bottom;
        int i, j;
        
        i=0; j=y;
        while (i<=j) {
            int m=(i+j)/2;
            if (column(image, m)) j=m-1; else i=m+1;
        }
        left = i;
        
        i=y; j=image[x].length-1;
        while (i<=j) {
            int m=(i+j)/2;
            if (column(image, m)) i=m+1; else j=m-1;
        }
        right = j;
        
        i=0; j=x;
        while (i<=j) {
            int m=(i+j)/2;
            if (row(image, m)) j=m-1; else i=m+1;
        }
        top = i;
        
        i=x; j=image.length-1;
        while (i<=j) {
            int m=(i+j)/2;
            if (row(image, m)) i=m+1; else j=m-1;
        }
        bottom = j;
        
        return (right-left+1) * (bottom-top+1);
    }
}



评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值