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: Depth First Search (DFS), Breadth First Search (BFS) and Binary Search
Intuition
Traversal all the pixels. Keep the maximum and minimum values of black pixels coordinates.
Algorithm
We keep four boundaries, left
, right
, top
and bottom
of the rectangle.
Note that left
and top
are inclusive while right
and bottom
are exclusive.
We then traversal all the pixels and update the four boundaries accordingly.
The recipe is following:
(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
Time complexity : . and are the height and width of the image.
Space complexity : . All we need to store are the four boundaries.
Comment One may optimize this algorithm to stop early. But it doesn't change the asymptotic performance. 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.
Intuition
Explore all the connected black pixel from the given pixel and update the boundaries.
Algorithm
The naive approach did not use the condition that all the black pixels are connected and that one of the black pixels is given.
A 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.
Complexity Analysis
Here 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, .
The space complexity is where is the number of vertices in the traversed graph. In this problem . Again, in the worst case, .
Comment
Although 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.
Intuition
Project the 2D image into a 1D array and use binary search to find the boundaries.
Algorithm
*Figure 1. Illustration of image projection.
Suppose 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
If a column has any black pixel it's projection is black otherwise white.
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.
Now, we claim the following lemma:
Lemma
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
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". Therefore, we conclude that all the black pixels in the 1D projection array are connected.
With this lemma, we have the following algorithm:
left
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.
The 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".
Recall the binary search algorithm in a 1D array, each time we only check one element, the pivot, to decide which half we go next.
In 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.
In 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
.
Similarly it costs to find top
and bottom
. The entire algorithm has a time complexity of
Complexity Analysis
Here, 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.
Both binary search and linear search used only constant extra space.