-
Notifications
You must be signed in to change notification settings - Fork 7
302. Smallest Rectangle Enclosing Black Pixels #356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
302. Smallest Rectangle Enclosing Black Pixels #356
Conversation
| if (x < left) { | ||
| left = x; | ||
| } | ||
| if (y < bottom) { | ||
| bottom = y; | ||
| } | ||
| if (y > top) { | ||
| top = y; | ||
| } | ||
| if (x > right) { | ||
| right = x; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although programmatically it is not a problem, it would be better to keep the coordinate notation right:
| if (x < left) { | |
| left = x; | |
| } | |
| if (y < bottom) { | |
| bottom = y; | |
| } | |
| if (y > top) { | |
| top = y; | |
| } | |
| if (x > right) { | |
| right = x; | |
| } | |
| if (y < left) { | |
| left = y; | |
| } | |
| if (x < bottom) { | |
| bottom = x; | |
| } | |
| if (x > top) { | |
| top = x; | |
| } | |
| if (y > right) { | |
| right = y; | |
| } | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think considering x to be vertical and y to be horizontal is less suitable.
| while (k < high && isBlack(mid, k, isRow)) { | ||
| k++; | ||
| } | ||
| if ((k < high) == findMin) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
findMin is quite a smart idea to generalize the Binary Search method for both rows and cols.
altay9
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your valuable contribution Erdem.
Approved.
Resolves: #219
Recommended problems before this:
200. Number of Islands : https://leetcode.com/problems/number-of-islands/
1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold: #358
1765. Map of Highest Peak: #357
Algorithm:
The smallest rectangle area would correspond to a reactangle covering the left- and right-most points of the black pixels as well as the top and the bottom boundaries.
Approach 1-Binary Search
The intuition here is that the black pixel at the bottom is located somewhere between the given x-coordinate (coordinates for 2D-arrays aren't defined properly) and 0. If we find a black pixel in any of the pixels in the row between these, that means that we must look further down to see if there is a row with a black pixel in it. Else, we look between the middle and the x. This naturally corresponds to binary-searching all the rows between these. For the upper case, we always try to look for the upper rows. For left and right, our boundaries are not just the matrix boundaries, but the already calculated bottom and top values. While searching columns, we don't look for the pixels in those areas.
Approach 2-DFS
This approach is more straight forward but is slower, especially in cases where the majority of the image is black pixels. Since all the black pixels are connected, we can just search through the given pixel to find the boundaries. We can also mark visited nodes to
'1'to save that they are visited.