Skip to content

Conversation

@ErdemT09
Copy link
Collaborator

@ErdemT09 ErdemT09 commented Jul 5, 2021

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.

@ErdemT09 ErdemT09 marked this pull request as ready for review July 5, 2021 08:44
Comment on lines +29 to +40
if (x < left) {
left = x;
}
if (y < bottom) {
bottom = y;
}
if (y > top) {
top = y;
}
if (x > right) {
right = x;
}
Copy link
Collaborator

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:

Suggested change
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;
}

Copy link
Collaborator Author

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) {
Copy link
Collaborator

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.

Copy link
Collaborator

@altay9 altay9 left a 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.

@ErdemT09 ErdemT09 merged commit 15f47af into master Jul 8, 2021
@ErdemT09 ErdemT09 deleted the 302.-Smallest-Rectangle-Enclosing-Black-Pixels branch July 8, 2021 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

302. Smallest Rectangle Enclosing Black Pixels

3 participants