Tuesday, January 12, 2016

Leetcode: 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 = 0y = 2,
Return 6.
DFS Solution:
Look for the borders for the black pixels. 

Code (Java):
public class Solution {
    private int top;
    private int bottom;
    private int left;
    private int right;
    private int area = 0;
    
    public int minArea(char[][] image, int x, int y) {
        if (image == null || image.length == 0) {
            return 0;
        }
        
        this.top = y;
        this.bottom = y;
        this.left = x;
        this.right = x;
        
        int m = image.length;
        int n = image[0].length;
        
        boolean[][] visited = new boolean[m][n];
        
        minAreaHelper(image, x, y, visited);
        
        return area;
    }
    
    private void minAreaHelper(char[][] image, int x, int y, 
                               boolean[][] visited) {
        int m = image.length;
        int n = image[0].length;
        
        if (x < 0 || x >= m || y < 0 || y >= n || visited[x][y]) {
            return;
        }          
        
        if (image[x][y] == '0') {
            return;
        }
        
        visited[x][y] = true;
        
        // update the border 
        top = Math.min(top, y);
        bottom = Math.max(bottom, y);
        left = Math.min(left, x);
        right = Math.max(right, x);
        
        int curArea = (bottom - top + 1) * (right - left + 1);
        area = Math.max(area, curArea);
        
        minAreaHelper(image, x, y - 1, visited);
        minAreaHelper(image, x, y + 1, visited);
        minAreaHelper(image, x - 1, y, visited);
        minAreaHelper(image, x + 1, y, visited);
    }
}

Update on 4/2/19:
public class Solution {
    /**
     * @param image: a binary matrix with '0' and '1'
     * @param x: the location of one of the black pixels
     * @param 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) {
            return 0;
        }

        int m = image.length;
        int n = image[0].length;

        int left = findLeft(image, 0, y);
        int right = findRight(image, y, n - 1);
        int top = findTop(image, 0, x);
        int bottom = findBottom(image, x, m - 1);

        return (right - left + 1) * (bottom - top + 1);
    }

    private int findLeft(char[][] image, int lo, int hi) {
        while (lo + 1 < hi) {
            int mid = lo + (hi - lo) / 2;
            if (isEmptyColumn(image, mid)) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }

        if (!isEmptyColumn(image, lo)) {
            return lo;
        }

        return hi;
    }

    private int findRight(char[][] image, int lo, int hi) {
        while (lo + 1 < hi) {
            int mid = lo + (hi - lo) / 2;
            if (isEmptyColumn(image, mid)) {
                hi = mid - 1;
            } else {
                lo = mid;
            }
        }

        if (!isEmptyColumn(image, hi)) {
            return hi;
        }
        return lo;
    }

    private int findTop(char[][] image, int lo, int hi) {
        while (lo + 1 < hi) {
            int mid = lo + (hi - lo) / 2;
            if (isEmptyRow(image, mid)) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }

        if (!isEmptyRow(image, lo)) {
            return lo;
        }

        return hi;
    }

    private int findBottom(char[][] image, int lo, int hi) {
        while (lo + 1 < hi) {
            int mid = lo + (hi - lo) / 2;

            if (isEmptyRow(image, mid)) {
                hi = mid - 1;
            } else {
                lo = mid;
            }
        }

        if (!isEmptyRow(image, hi)) {
            return hi;
        }

        return lo;
    }

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

        return true;
    }

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

        return true;
    }
}
Complexity:
O(mlogn + nlogm)

No comments:

Post a Comment