-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
| 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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.