Takefumi Yamamura's blog

This blog is for my memorandum about programming and English.

Happy Coding

This blog is for my memorandum

302. Smallest Rectangle Enclosing Black Pixels


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.

how to solve

I can sove this problem by blute force search. I have to memorize the minX, maxX , minY, maxY. Then the answer is (maxX - minX + 1) * (maxY - minY + 1).