forked from fu11211129/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b79792
commit a96f4ff
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
public class SmallestRectangleEnclosingBlackPixels { | ||
public int minArea(char[][] image, int x, int y) { | ||
int top = searchVertical(image, 0, x, true); | ||
int bottom = searchVertical(image, x, image.length - 1, false); | ||
int left = searchHorizontal(image, 0, y, true); | ||
int right = searchHorizontal(image, y, image[0].length - 1, false); | ||
|
||
int area = (bottom - top + 1) * (right - left + 1); | ||
return area; | ||
} | ||
|
||
public int searchVertical(char[][] image, int top, int bottom, boolean searchTop) { | ||
int re = searchTop? bottom: top; | ||
while(top <= bottom) { | ||
int mid = (top + bottom) / 2; | ||
int s = 0; | ||
// determine if this horizontal line contains 1 or not | ||
for(int j=0; j<image[mid].length; ++j) s += (image[mid][j] - '0'); | ||
if(s >= 1) { | ||
re = mid; | ||
if(searchTop) bottom = mid - 1; | ||
else top = mid + 1; | ||
} else { | ||
if(searchTop) top = mid + 1; | ||
else bottom = mid - 1; | ||
} | ||
} | ||
return re; | ||
} | ||
|
||
public int searchHorizontal(char[][] image, int left, int right, boolean searchLeft) { | ||
int re = searchLeft? right: left; | ||
while(left <= right) { | ||
int mid = (left + right) / 2; | ||
int s = 0; | ||
for(int i=0; i<image.length; ++i) s += (image[i][mid] - '0'); | ||
if(s >= 1) { | ||
re = mid; | ||
if(searchLeft) right = mid - 1; | ||
else left = mid + 1; | ||
} else { | ||
if(searchLeft) left = mid + 1; | ||
else right = mid - 1; | ||
} | ||
} | ||
return re; | ||
} | ||
} |