.# 302. Smallest Rectangle Enclosing Black Pixels (Hard)

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 1: Brute Force 16ms

class Solution {
public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        int up = x, down = x, left = y, right = y;
        for (int i = 0; i < image.size(); ++i) {
            for (int j = 0; j < image[i].size(); ++j) {
                if (image[i][j] == '1') {
                    up = min(up, i);
                    down = max(down, i);
                    left = min(left, j);
                    right = max(right, j);
                }
            }
        }

        return (right-left+1)*(down-up+1);
    }
};

Solution 2: DFS 19ms

class Solution {
    void dfs(vector<vector<char>>& image, int x, int y, int& up, int& down, int& left, int& right) {
        if (x < 0 || x >= image.size() || y < 0 || y >= image[0].size() || image[x][y] != '1') return;
        up = min(x,up);
        down = max(x,down);
        left = min(left,y);
        right = max(right,y);
        image[x][y] = '2';
        dfs(image, x-1, y, up, down, left, right);
        dfs(image, x+1, y, up, down, left, right);
        dfs(image, x, y-1, up, down, left, right);
        dfs(image, x, y+1, up, down, left, right);
    }

public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        int up = x, down = x, left = y, right = y;
        dfs(image, x, y, up, down, left, right);
        return (right-left+1)*(down-up+1);
    }
};

Solution 3: Binary Search 19ms

If we don't know programming, how do we find the 4 boundaries given a black pixel?

Do we need to search every black cell? Absolutely not.

Intuitively, we would expand from the given 1 * 1 black cell, "aggressively" expand to the 4 boundaries, roughly half of the remaining space. If we don't "cut" any black pixel, we know we go too far and should go back half.

This is exactly the process of binary search.

One simple way without any worry about boundary, is as follows:

  • Use a vertical line, to jump to the leftmost black pixel , in the range of [0, y]
  • Use a vertical line, to jump to the rightmost black pixel, in the range of [y, n - 1]
  • Use a horizontal line, to jump to the topmost black pixel, in the range of [0, x]
  • Use a horizontal line, to jump to the bottommost black pixel, in the range of [x, m - 1]
class Solution {
    int binarySearch(vector<vector<char>>& image, bool horizontal, int low, int high, int min, int max, bool goLower) {
        while (low < high) {
            int mid = (low+high)/2, i = min;
            while (i < max && (horizontal ? image[mid][i] : image[i][mid]) == '0') ++i;
            if ((i < max) == goLower) high = mid;
            else low = mid+1;
        }
        return low;
    }

public:
    int minArea(vector<vector<char>>& image, int x, int y) {
        int m = image.size(), n = image[0].size();
        int up = binarySearch(image, true, 0, x, 0, n, true);
        int down = binarySearch(image, true, x+1, m, 0, n, false);
        int left = binarySearch(image, false, 0, y, up, down, true);
        int right = binarySearch(image, false, y+1, n, up, down, false);
        return (right-left)*(down-up);
    }
};

results matching ""

    No results matching ""