302. Smallest Rectangle Enclosing Black Pixels [leetcode]

本文介绍了一种通过二分查找解决最小矩形面积问题的算法。该算法首先确定黑色像素区域的水平边界和垂直边界,然后计算边界之间的矩形面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

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


Solution


最主要是知道一个定理   refer : here

If there are only one black pixel region, then in a projected 1D array all the black pixels are connected.

这样就用正常的 binary search 就好了

class Solution {
    int getHorizan(const vector<vector<char>>& image, int left, int right,  bool flag){
        
        while(left <= right){
            int mid = (left + right)/2 , k = 0;
            
            while(  k < image.size() && image[k][mid] == '0')  k++;
            
            if( k != image.size()){
                flag ? right = mid - 1 : left = mid + 1;
            }
            else {
                flag ? left = mid + 1 : right = mid - 1;
            }
        }
        return flag ? left : right;
    }
    
    int getVertical(const vector<vector<char>>& image, int top, int bottom, bool flag){
        
        while( top <= bottom){
            int mid = ( top + bottom) / 2 , k = 0;
            
            while(k < image[0].size() && image[mid][k] == '0' ){
                k++;
            }
            
            if( k != image[0].size() ){
               flag ? bottom = mid -1 : top = mid + 1;
            }
            else {
               flag ? top = mid + 1 : bottom = mid - 1;
            }
        }
        return flag ? top : bottom;
    }
    
public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        if(image.empty() || image[0].empty()) return 0;
        int M = image.size(), N = image[0].size();
        
        int leftBoard = getHorizan(image, 0, y, true);
        int rightBoard = getHorizan(image, y, N - 1, false);
        
        int topBoard = getVertical(image, 0, x, true);
        int bottomBoard = getVertical(image, x, M -1, false);

        
        return (rightBoard - leftBoard + 1) *( bottomBoard - topBoard + 1);
    }
};

Solution 2 

还有一个比较straigtforward的方法,就是  bfs
时间复杂度明显不如binary search 
需要注意几点

1. unordered_set 中的key不能是pair
2. 计算 visited 的idx时 , x*N, 而不是 x*M
class Solution {
public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        const int M = image.size(), N = image[0].size();
        
        queue<pair<int,int>> toVisit;
        vector<bool> visited ( M*N, false);
        
        vector<pair<int,int>> lib = {
            make_pair(0,1), make_pair(0,-1), make_pair(-1,0), make_pair(1,0)
        };
        
        int leftMost = y, rightMost = y , upMost = x, downMost = x;
        toVisit.push(make_pair(x,y));
        int idx = x*N + y;
        visited[idx] = true;
        
        while(!toVisit.empty()){
            pair<int,int> curPair = toVisit.front();
            int curX = curPair.first, curY = curPair.second;
            toVisit.pop();
            leftMost = min(leftMost, curY);
            rightMost = max(rightMost, curY);
            upMost = min(upMost, curX);
            downMost = max(downMost, curX);
            
            for( auto p : lib){
                int nextX = curX + p.first, nextY = curY + p.second;
                if(nextX == -1 ||nextX == M || nextY == N || nextY == -1) continue;
                
                int idx = nextX*N + nextY;
                if(!visited[idx] && image[nextX][nextY] == '1') {
                    toVisit.push(make_pair(nextX, nextY));
                    visited[idx] = true;
                }
            }
        }
        
        return (rightMost - leftMost + 1) * (downMost - upMost + 1);
        
    }
};
     





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值