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
Example 1:
Input:["0010","0110","0100"],x=0,y=2
Output:6
Explanation:
The upper left coordinate of the matrix is (0,1), and the lower right coordinate is (2,2).
Example 2:
Input:["1110","1100","0000","0000"], x = 0, y = 1
Output:6
Explanation:
The upper left coordinate of the matrix is (0, 0), and the lower right coordinate is (1,2).
思路:可以用BFS,也就是连通图,找所有相连的点的x,y的最大值,最小值;O(n*m);
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 || image[0].length == 0) {
return 0;
}
int n = image.length;
int m = image[0].length;
boolean[][] visited = new boolean[n][m];
Queue<Integer> queueX = new LinkedList<Integer>();
Queue<Integer> queueY = new LinkedList<Integer>();
queueX.offer(x);
queueY.offer(y);
visited[x][y] = true;
int maxX = x; int maxY = y;
int minX = x; int minY = y;
int[] dx = {0,0,1,-1};
int[] dy = {1,-1,0,0};
while(!queueX.isEmpty()){
int i = queueX.poll();
int j = queueY.poll();
for(int k = 0 ; k < 4; k++) {
int nx = i + dx[k];
int ny = j + dy[k];
if(0 <= nx && nx < n && 0 <= ny && ny < m && !visited[nx][ny]
&& image[nx][ny] == '1') {
visited[nx][ny] = true;
queueX.offer(nx);
queueY.offer(ny);
maxX = Math.max(maxX, nx);
minX = Math.min(minX, nx);
maxY = Math.max(maxY, ny);
minY = Math.min(minY, ny);
}
}
}
return (maxX - minX + 1) * (maxY - minY + 1);
}
}
思路2:这题更牛逼的解法是用二分法去找top, bottom, left, right的边界,也就是找最后一个不能使得row或者col为空的值;
O(n * logm + m * logn);
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 || image[0].length == 0) {
return 0;
}
int n = image.length;
int m = image[0].length;
int top = findTop(image, 0, x);
int bottom = findBottom(image, x, n-1);
int left = findLeft(image, 0, y);
int right = findRight(image, y, m-1);
return (bottom - top + 1) * (right - left + 1);
}
private int findTop(char[][] image, int start, int end) {
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(!isRowEmpty(image, mid)){
end = mid;
} else{
start = mid;
}
}
if(isRowEmpty(image, start)){
return end;
}
return start;
}
private int findBottom(char[][] image, int start, int end) {
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(!isRowEmpty(image, mid)){
start = mid;
} else{
end = mid;
}
}
if(isRowEmpty(image, end)){
return start;
}
return end;
}
private int findLeft(char[][] image, int start, int end) {
while(start + 1 < end) {
int mid = start + (end - start) / 2;
if(!isColEmpty(image, mid)){
end = mid;
} else {
start = mid;
}
}
if(isColEmpty(image, start)){
return end;
}
return start;
}
private int findRight(char[][] image, int start, int end) {
while(start + 1 < end) {
int mid = start + (end - start)/ 2;
if(!isColEmpty(image, mid)){
start = mid;
} else {
end = mid;
}
}
if(isColEmpty(image, end)){
return start;
}
return end;
}
private boolean isRowEmpty(char[][] image, int i) {
for(int j = 0; j < image[0].length; j++){
if(image[i][j] != '0'){
return false;
}
}
return true;
}
private boolean isColEmpty(char[][] image, int j) {
for(int i = 0; i < image.length; i++) {
if(image[i][j] != '0'){
return false;
}
}
return true;
}
}