An image is represented by a binary matrix with0as a white pixel and1as 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.
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
*/
private int m, n;
public int minArea(char[][] image, int x, int y) {
// write your code here
m = image.length;
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 start, int end) {
while (start + 1 < end) {
int mid = (end - start) / 2 + start;
if (isColEmpty(image, mid)) {
start = mid;
} else {
end = mid;
}
}
if (!isColEmpty(image, start)) {
return start;
}
return end;
}
private int findRight(char[][] image, int start, int end) {
while (start + 1 < end) {
int mid = (end - start) / 2 + start;
if (isColEmpty(image, mid)) {
end = mid;
} else {
start = mid;
}
}
if (!isColEmpty(image, end)) {
return end;
}
return start;
}
private int findTop(char[][] image, int start, int end) {
while (start + 1 < end) {
int mid = (end - start) / 2 + start;
if (isRowEmpty(image, mid)) {
start = mid;
} else {
end = mid;
}
}
if (!isRowEmpty(image, start)) {
return start;
}
return end;
}
private int findBottom(char[][] image, int start, int end) {
while (start + 1 < end) {
int mid = (end - start) / 2 + start;
if (isRowEmpty(image, mid)) {
end = mid;
} else {
start = mid;
}
}
if (!isRowEmpty(image, end)) {
return end;
}
return start;
}
private boolean isRowEmpty(char[][] image, int row) {
for (int j = 0; j < n; j++) {
if (image[row][j] == '1') {
return false;
}
}
return true;
}
private boolean isColEmpty(char[][] image, int col) {
for (int i = 0; i < m; i++) {
if (image[i][col] == '1') {
return false;
}
}
return true;
}
}