302. Smallest Rectangle Enclosing Black Pixels - cocoder39/coco39_LC GitHub Wiki
302. Smallest Rectangle Enclosing Black Pixels
cite from leetcode discussion
Suppose we have a 2D array
"000000111000000"
"000000101000000"
"000000101100000"
"000001100100000"
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
"000001111100000"
Theorem
If there are only one black pixel region, then in a projected 1D array all the black pixels are connected.
Proof by contradiction
Assume to the contrary that there are disconnected black pixels at i and j where i < j in the 1D projection array. Thus there exists one column k, k in (i, j) and and the column k in the 2D array has no black pixel. Therefore in the 2D array there exists at least 2 black pixel regions separated by column k which contradicting the condition of "only one black pixel region".
Therefore we conclude that all the black pixels in the 1D projection array is 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)
class Solution {
public:
int minArea(vector<vector<char>>& image, int x, int y) {
int m = image.size();
int n = image[0].size();
int top = searchRow(image, 0, x, 0, n - 1, false); //[0, 0] to [x, n]
int bottom = searchRow(image, x, m - 1, 0, n - 1, true); //[x, 0] to [m, n]
int left = searchCol(image, top, bottom, 0, y, false); //[top, 0] to [x, y]
int right = searchCol(image, top, bottom, y, n - 1, true); //[x, y] to [down, n]
cout << left << right << top << bottom << endl;
return (bottom - top + 1) * (right - left + 1);
}
private:
int searchRow(vector<vector<char>>& image, int top, int bottom, int left, int right, bool top2bottom) {
int start = top, end = bottom;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
bool found = false;
for (int i = left; i <= right; i++) {
if (image[mid][i] == '1') {
found = true;
break;
}
}
if (top2bottom ^ found) // top2bottom && ! found || ! top2bottom && found
end = mid;
else // top2bottom && found || ! top2bottom && ! found
start = mid;
}
if (top2bottom) {
for (int i = left; i <= right; i++) {
if (image[end][i] == '1')
return end;
}
return start;
}
else {
for (int i = left; i <= right; i++) {
if (image[start][i] == '1')
return start;
}
return end;
}
}
int searchCol(vector<vector<char>>& image, int top, int bottom, int left, int right, bool left2right) {
int start = left, end = right;
while (start + 1 < end) {
int mid = start + (end - start) / 2;
cout << mid << endl;
bool found = false;
for (int i = top; i <= bottom; i++) {
if (image[i][mid] == '1') {
found = true;
break;
}
}
if (left2right ^ found) // top2bottom && found || ! top2bottom && ! found
end = mid;
else // top2bottom && ! found || ! top2bottom && found
start = mid;
}
if (left2right) {
for (int i = top; i <= bottom; i++) {
if (image[i][end] == '1')
return end;
}
return start;
}
else {
for (int i = top; i <= bottom; i++) {
if (image[i][start] == '1')
return start;
}
return end;
}
}
};