# 302.Smallest Rectangle enclosing Black Pixels

Source: Internet
Author: User

Topic:

An image was represented by a binary matrix with as a white pixel and as `0` `1` a black pixel. The black pixels is connected, i.e., there was only one black region. Pixels is connected horizontally and vertically. Given the location `(x, y)` of the one of the black pixels, return to the area of the smallest (axis-aligned) rectangle that enclose s All Black pixels.

For example, given the following image:

`[  "0010",  "0110",  "0100"]`

and `x = 0` , `y = 2`

Return `6` .

Exercises

Find the smallest rectangle that contains all black pixel. Here we use two points to find. Because a black pixel point (x, y) is given, and all black pixel are connected, in the case of row search, all column containing black pixel must be contiguous when mapped to row X. So we can use binary search to search for a column with black pixel on the far left in 0 to Y. You can then continue searching the upper and lower bounds. When searching the right and bottom boundaries, we're looking for the first ' 0 ', so we're going to pass a Boolean variable Searchlo to judge.

Time Complexity-o (Mlogn + NLOGM), Space complexity-o (1)

` Public classSolution { Public intMinarea (Char[] Image,intXinty) {if(Image = =NULL|| Image.length = = 0) {            return0; }        intRowNum = image.length, Colnum = image[0].length; intleft = BinarySearch (image, 0, y, 0, RowNum,true,true); intright = BinarySearch (image, Y + 1, colnum, 0, RowNum,true,false); inttop = BinarySearch (image, 0, X, left, right,false,true); intbot = BinarySearch (image, X + 1, RowNum, left, right,false,false); return(right-left) * (bot-top); }        Private intBinarySearch (Char[] Image,intLointHiintMinintMaxBooleanSearchhorizontal,BooleanSearchlo) {         while(Lo <hi) {            intMid = lo + (Hi-lo)/2; BooleanHasblackpixel =false;  for(inti = min; i < Max; i++) {                if(Searchhorizontal Image[i][mid]: image[mid][i]) = = ' 1 ') {Hasblackpixel=true;  Break; }            }            if(Hasblackpixel = =Searchlo) {Hi=mid; } Else{lo= Mid + 1; }        }        returnLo; }}`

Reference:

Https://leetcode.com/discuss/68246/c-java-python-binary-search-solution-with-explanation

Https://leetcode.com/discuss/71898/java-concise-binary-search-4x-faster-than-dfs

Https://leetcode.com/discuss/68407/clear-binary-search-python

Https://leetcode.com/discuss/68233/java-dfs-solution-and-seeking-for-a-binary-search-solution

Https://leetcode.com/discuss/70670/dfs-bfs-binary-search-and-brute-force-interation

302.Smallest Rectangle enclosing Black Pixels

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.