[LeetCode][Search] 302. Smallest Rectangle Enclosing Black Pixels

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

DFS搜索所有与当前1连通的点。minX, maxX, minY, maxY用来记录轮廓,最后计算出rectangle的大小。

class Solution {
private:
    int minX, maxX, minY, maxY;
    vector<vector<bool>> canUse;
    int step[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:
    void search(vector<vector<char>>& image, int x, int y) {
        canUse[x][y] = false;
        minX = min(minX, x);
        maxX = max(maxX, x);
        minY = min(minY, y);
        maxY = max(maxY, y);
        for(int i = 0; i < 4; i++) {
            int newX = step[i][0] + x;
            int newY = step[i][1] + y;
            if (0 <= newX && newX < image.size() && 0 <= newY && newY < image[0].size() 
            && canUse[newX][newY] && image[newX][newY] == '1') {
                search(image, newX, newY);
            }
        }
    }
    
    int minArea(vector<vector<char>>& image, int x, int y) {
        if (image.size() == 0) {
            return 0;
        }
        
        minX = minY = INT_MAX;
        maxX = maxY = INT_MIN;
        vector<bool> a(image[0].size(), true);
        for(int i = 0; i < image.size(); i++) {
            canUse.push_back(a);
        }
        search(image, x, y);
        
        return (maxX - minX + 1) * (maxY - minY + 1);
    }
};

还有一种BST的方法,参考这里。主要思想是分别对行和列BS,例如对行search,每次要搜索所有元素找到只要有一个为1就算连通。具体参考代码如下:

class Solution {
public:
    int searchX(vector<vector<char>>& image, int beg, int end, bool findMin) {
        if (beg > end) {
            return -1;
        }
        
        int mid = beg + (end - beg) / 2;
        for(int i = 0; i < image[mid].size(); i++) {
            if (image[mid][i] == '1') {
                int index = findMin 
                ? searchX(image, beg, mid - 1, findMin)
                : searchX(image, mid + 1, end, findMin);
                return index != -1 ? index : mid;
            }
        }
        
        return findMin
        ? searchX(image, mid + 1, end, findMin)
        : searchX(image, beg, mid - 1, findMin);
    }
    
    int searchY(vector<vector<char>>& image, int beg, int end, bool findMin) {
        if (beg > end) {
            return -1;
        }
        
        int mid = beg + (end - beg) / 2;
        cout << "beg:" << beg << endl;
        cout << "end:" << end << endl;
        cout << "mid:" << mid << endl;
        for(int i = 0; i < image.size(); i++) {
            if (image[i][mid] == '1') {
                int index = findMin 
                ? searchY(image, beg, mid - 1, findMin)
                : searchY(image, mid + 1, end, findMin);
                return index != -1 ? index : mid;
            }
        }
        
        return findMin
        ? searchY(image, mid + 1, end, findMin)
        : searchY(image, beg, mid - 1, findMin);        
    }
    
    int minArea(vector<vector<char>>& image, int x, int y) {
        if (image.size() == 0 || image[0].size() == 0) {
            return 0;
        }
        int minX = searchX(image, 0, x, true);
        int maxX = searchX(image, x, image.size() - 1, false);
        int minY = searchY(image, 0, y, true);
        int maxY = searchY(image, y, image[0].size() - 1, false);
        
        return (maxX - minX + 1) * (maxY - minY + 1);
    }
};
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 期盼着的班级采风终于来到 在老师的组织带领下,我们正在通往江西――婺源,中国最美乡村。 刚刚在想,出游一趟的意义在...
    一言不语阅读 1,553评论 0 0
  • 有幸诗坛闻墨香,只缘网络架津梁。 群英荟萃抒情趣,众艳芬芳耀眼光。 欲效临池闲绪遣,不求艺苑美名彰。 陶情养性修身...
    开宗明义阅读 2,144评论 0 1
  • 聊一聊 敏捷管理 每种管理的方式都有边界 在实际的管理de过程中却有相通之处 其实敏捷开发是一种过程控制论,通俗的...
    后龙山大掌柜阅读 2,507评论 0 2