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:

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

Solution:

Using Binary search to find top, bottom, left and right black pixels.

public class Solution {
    /**
     * @param image a binary matrix with '0' and '1'
     * @param x, y the location of one of the black pixels
     * @return an integer
     */
    public int minArea(char[][] image, int x, int y) {
        if  (image == null || image.length == 0 || image[0].length == 0) {
            return 0;
        }
        //find most right
        int start = y;
        int end = image[0].length - 1;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (checkCol(image, mid)) {
                start = mid;
            } else {
                end = mid - 1;
            }
        }
        int right = 0;
        if (checkCol(image, end)) {
            right = end;
        } else {
            right = start;
        }

        //find most left;
        start = 0;
        end = y;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (checkCol(image, mid)) {
                end = mid;
            } else {
                start = mid + 1;
            }
        }
        int left = 0;
        if (checkCol(image, start)) {
            left = start;
        } else {
            left = end;
        }
        //find top
        start = 0;
        end = x;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (checkRow(image, mid)) {
                end = mid;
            } else {
                start = mid + 1;
            }
        }
        int top = 0;
        if (checkRow(image, start)) {
            top = start;
        } else {
            top = end;
        }
        //find bottom
        start = x;
        end = image.length - 1;
        while (start + 1 < end) {
            int mid = start + (end - start) / 2;
            if (checkRow(image, mid)) {
                start = mid;
            } else {
                end = mid - 1;
            }
        }
        int bottom = 0;
        if (checkRow(image, end)) {
            bottom = end;
        } else {
            bottom = start;
        }
        return (right - left + 1) * (bottom - top + 1);
    }

    private boolean checkCol(char[][] image, int col) {
        for (int i = 0; i < image.length; i++) {
            if (image[i][col] == '1') {
                return true;
            }
        }
        return false;
    }

    private boolean checkRow(char[][] image, int row) {
        for (int i = 0; i < image[row].length; i++) {
            if (image[row][i] == '1') {
                return true;
            }
        }
        return false;
    }
}

results matching ""

    No results matching ""