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
.
This article is for intermediate readers. It introduces the following ideas:\nDepth First Search (DFS), Breadth First Search (BFS) and Binary Search
\nIntuition
\nTraversal all the pixels. Keep the maximum and minimum values of black pixels coordinates.
\nAlgorithm
\nWe keep four boundaries, left
, right
, top
and bottom
of the rectangle.\nNote that left
and top
are inclusive while right
and bottom
are exclusive.\nWe then traversal all the pixels and update the four boundaries accordingly.
The recipe is following:
\n(x, y)
coordinatesimage[x][y]
is blackleft = min(left, x)
right = max(right, x + 1)
top = min(top, y)
bottom = max(bottom, y + 1)
(right - left) * (bottom - top)
Complexity Analysis
\nTime complexity : . and are the height and width of the image.
\nSpace complexity : . All we need to store are the four boundaries.
\nComment\n One may optimize this algorithm to stop early. But it doesn\'t change the asymptotic performance.\n This naive approach is certainly not the best answer to this problem. However, it gives you a good entry point to tackle the problem. Most of the time the good algorithms come from identifying the repeat calculation a naive approach. And it also sets up a baseline of the time and space complexity, so that one can see whether or not other approaches are better than it.
\nIntuition
\nExplore all the connected black pixel from the given pixel and update the boundaries.
\nAlgorithm
\nThe naive approach did not use the condition that all the black pixels are connected and that one of the black pixels is given.
\nA simple way to use these facts is to do an exhaustive search starting from the given pixel. Since all the black pixels are connected, DFS or BFS will visit all of them starting from the given black pixel. The idea is similar to what we did for 200. Number of Island. Instead of many islands, we have only one island here, and we know one pixel of it.
\n\nComplexity Analysis
\nHere is the number of edges in the traversed graph. is the total number of black pixels. Since each pixel have four edges at most, . In the worst case, .
\nThe space complexity is where is the number of vertices in the traversed graph. In this problem . Again, in the worst case, .
\nComment
\nAlthough this approach is better than naive approach when is much smaller than , it is asymptotically the same as approach #1 when is comparable to . And it costs a lot more auxiliary space.
\nIntuition
\nProject the 2D image into a 1D array and use binary search to find the boundaries.
\nAlgorithm
\n*Figure 1. Illustration of image projection.
\nSuppose we have a image as shown in figure 1, if we project each column of the image into an entry of row vector v
with the following rule:
v[i] = 1
if exists x
such that image[x][i] = 1
v[i] = 0
otherwiseThat is
\n\n\nIf a column has any black pixel it\'s projection is black otherwise white.
\n
Similarly, we can do the same for the rows, and project the image into a 1D column vector. The two projected vectors are shown in figure 1.
\nNow, we claim the following lemma:
\nLemma
\n\n\nIf there are only one black pixel region, then in a projected 1D array all the black pixels are connected.
\n
Proof by contradiction
\n\n\nAssume to the contrary that there are disconnected black pixels at
\ni
andj
wherei < j
in the 1D projection array. Thus, there exists one columnk
,k
in(i, j)
and the columnk
in the 2D array has no black pixel. Therefore, in the 2D array there exist at least two black pixel regions separated by columnk
which contradicting the condition of "only one black pixel region".\nTherefore, we conclude that all the black pixels in the 1D projection array are connected.
With this lemma, we have the following algorithm:
\nleft
in the row array within [0, y)
right
in the row array within [y + 1, n)
top
in the column array within [0, x)
bottom
in the column array within [x + 1, m)
(right - left) * (bottom - top)
However, the projection step cost time which dominates the entire algorithm.If so, we gain nothing comparing with previous approaches.
\nThe trick is that we do not need to do the projection step as a preprocess. We can do it on the fly, i.e. "don\'t project the column/row unless needed".
\nRecall the binary search algorithm in a 1D array, each time we only check one element, the pivot, to decide which half we go next.
\nIn a 2D array, we can do something similar. The only difference here is that the element is not a number but a vector. For example, a m
by n
matrix can be seen as n
column vectors.
In these n
elements/vectors, we do a binary search to find left
or right
. Each time we only check one element/vector, the pivot, to decide which half we go next.\nIn total it checks vectors, and each check is (we simply traverse all the m
entries of the pivot vector).
So it costs to find left
and right
.\nSimilarly it costs to find top
and bottom
. The entire algorithm has a time complexity of \n
Complexity Analysis
\nHere, and are the height and width of the image. We embedded a linear search for every iteration of binary search. See previous sections for details.
\nBoth binary search and linear search used only constant extra space.
\n