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.
Imagine we project the 2D array to the bottom axis with the rule "if a column has any black pixel it's projection is black otherwise white". The projected 1D array is "0110"
Theorem
If there are only one black pixel region, then in a projected 1D array all the black pixels are connected.
This means we can do a binary search in each half to find the boundaries, if we know one black pixel's position. And we do know that.
To find the left boundary, do the binary search in the [0, y)
range and find the first column vector who has any black pixel.
To determine if a column vector has a black pixel is O(m)
so the search in total is O(m log n)
We can do the same for the other boundaries. The area is then calculated by the boundaries. Thus the algorithm runs in O(m log n + n log m)
follow up是怎么把findLeft和findRight合并成一个:从颜色角度想,其实都是找左边界,只不过分别是找黑色格子左边界和白色格子左边界,所以都是找左边进,一个function就可以,只要控制输入的一个是黑格子一个是白格子
1 public class Solution { 2 public int minArea(char[][] image, int x, int y) { 3 int m = image.length; 4 int n = image[0].length; 5 int left = searchLeft(image, y); 6 int right = searchRight(image, y); 7 int top = searchTop(image, x); 8 int bottom = searchBottom(image, x); 9 return (right-left+1)*(bottom-top+1); 10 } 11 12 public int searchLeft(char[][] image, int right) { 13 int l = 0, r = right; 14 while (l <= r) { 15 int m = (l+r)/2; 16 if (!findBlack(image, m, false)) l = m+1; 17 else r = m-1; 18 } 19 return l; 20 } 21 22 public int searchRight(char[][] image, int left) { 23 int l = left, r = image[0].length-1; 24 while (l <= r) { 25 int m = (l+r)/2; 26 if (!findBlack(image, m, false)) r = m-1; 27 else l = m+1; 28 } 29 return r; 30 } 31 32 public int searchTop(char[][] image, int bottom) { 33 int l = 0, r = bottom; 34 while (l <= r) { 35 int m = (l+r)/2; 36 if (!findBlack(image, m, true)) l = m+1; 37 else r = m-1; 38 } 39 return l; 40 } 41 42 public int searchBottom(char[][] image, int top) { 43 int l=top, r=image.length-1; 44 while (l <= r) { 45 int m = (l+r)/2; 46 if (!findBlack(image, m, true)) r = m-1; 47 else l = m+1; 48 } 49 return r; 50 } 51 52 public boolean findBlack(char[][] image, int cur, boolean row) { 53 int m = image.length; 54 int n = image[0].length; 55 if (row) { 56 for (int i=0; i<n; i++) { 57 if (image[cur][i] == '1') 58 return true; 59 } 60 return false; 61 } 62 else { 63 for (int j=0; j<m; j++) { 64 if (image[j][cur] == '1') 65 return true; 66 } 67 return false; 68 } 69 } 70 }
合并findLeft和findRight, 合并findTop和findBottom
1 public class Solution { 2 char[][] image; 3 int m; 4 int n; 5 public int minArea(char[][] iImage, int x, int y) { 6 image = iImage; 7 m = image.length; 8 n = image[0].length; 9 int left = searchColumn(0, y, true); 10 int right = searchColumn(y, n-1, false); 11 int top = searchRow(0, x, true); 12 int bottom = searchRow(x, m-1, false); 13 return (right-left+1)*(bottom-top+1); 14 } 15 16 public int searchColumn(int start, int end, boolean isLeft) { 17 int l=start, r=end; 18 while (l <= r) { 19 int mid = (l+r)/2; 20 int k; 21 for (k=0; k<m; k++) { 22 if (image[k][mid] == '1') break; 23 } 24 if (isLeft) { 25 if (k<m) r=mid-1; 26 else l=mid+1; 27 } 28 else { 29 if (k<m) l=mid+1; 30 else r=mid-1; 31 } 32 } 33 return isLeft? l : r; 34 } 35 36 public int searchRow(int start, int end, boolean isTop) { 37 int l=start, r=end; 38 while (l<=r) { 39 int mid = (l+r)/2; 40 int k; 41 for (k=0; k<n; k++) { 42 if (image[mid][k] == '1') break; 43 } 44 if (isTop) { 45 if (k<n) r=mid-1; 46 else l=mid+1; 47 } 48 else { 49 if (k<n) l=m+1; 50 else r=m-1; 51 } 52 } 53 return isTop? l : r; 54 } 55 }