Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package algorithms.curated170.hard.smallestrectangleenclosingblackpixels;

public class SmallestRectangleEnclosingBlackPixelsBinarySearch {

private char[][] image;

public int minArea(char[][] image, int x, int y) {
this.image = image;
int m = image.length, n = image[0].length;

int bottom = searchBoundary(0, x, 0, n, true, true);
int top = searchBoundary(x + 1, m, 0, n, false, true);
int left = searchBoundary(0, y, bottom, top, true, false);
int right = searchBoundary(y + 1, n, bottom, top, false, false);

return (right - left) * (top - bottom);
}

private int searchBoundary(int i, int j, int low, int high, boolean findMin, boolean isRow) {
while (i != j) {
int k = low, mid = (i + j) / 2;
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.

j = mid;
} else {
i = mid + 1;
}
}

return i;
}

private boolean isBlack(int mid, int k, boolean isRow) {
return ((isRow) ? image[mid][k] : image[k][mid]) == '0';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package algorithms.curated170.hard.smallestrectangleenclosingblackpixels;

public class SmallestRectangleEnclosingBlackPixelsDFS {

int left, right, bottom, top;
int[][] directions = new int[][] { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };

public int minArea(char[][] image, int x, int y) {
left = 1000000;
right = 0;
bottom = 1000000;
top = 0;
findRectangleBoundaries(image, x, y);
return (right - left + 1) * (top - bottom + 1);
}

private void findRectangleBoundaries(char[][] image, int x, int y) {
if (isOutOfBoundariesOrBlack(image, x, y)) {
return;
}

image[x][y] = '0';

for (int[] dir : directions) {
int nx = x + dir[0];
int ny = y + dir[1];
findRectangleBoundaries(image, nx, ny);
}
if (x < left) {
left = x;
}
if (y < bottom) {
bottom = y;
}
if (y > top) {
top = y;
}
if (x > right) {
right = x;
}
Comment on lines +29 to +40
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.

}

private boolean isOutOfBoundariesOrBlack(char[][] image, int x, int y) {
return (x < 0 || y < 0 || x >= image.length || y >= image[0].length || image[x][y] == '0');
}
}