Skip to content

Commit 9d8ce18

Browse files
Smallest Rectangle Enclosing Black Pixels
1 parent a91dda6 commit 9d8ce18

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ My accepted leetcode solutions to some of the common interview problems.
104104
- [Island Perimeter](problems/src/depth_first_search/IslandPerimeter.java) (Easy)
105105
- [Number of Distinct Islands](problems/src/depth_first_search/NumberOfDistinctIslands.java) (Medium)
106106
- [Number of Distinct Islands II](problems/src/depth_first_search/NumberOfDistinctIslandsII.java) (Hard)
107+
- [Smallest Rectangle Enclosing Black Pixels](problems/src/depth_first_search/SmallestRectangleEnclosingBlackPixels.java) (Hard)
107108

108109
#### [Design](problems/src/design)
109110

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package depth_first_search;
2+
3+
/**
4+
* Created by gouthamvidyapradhan on 24/06/2018.
5+
* An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are
6+
* connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the
7+
* location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that
8+
* encloses all black pixels.
9+
10+
Example:
11+
12+
Input:
13+
[
14+
"0010",
15+
"0110",
16+
"0100"
17+
]
18+
and x = 0, y = 2
19+
20+
Output: 6
21+
22+
Solution: O(n x m) do a dfs and keep track of min and max length-breadth. Return the product of l x b
23+
*/
24+
public class SmallestRectangleEnclosingBlackPixels {
25+
private final int[] R = {1, -1, 0, 0};
26+
private final int[] C = {0, 0, -1, 1};
27+
private boolean[][] done;
28+
private int maxR, minR, minC, maxC;
29+
public static void main(String[] args) {
30+
char[][] A = {{'0', '0', '1', '1'}, {'0', '1', '1', '0'}, {'0', '1', '0', '0'}};
31+
System.out.println(new SmallestRectangleEnclosingBlackPixels().minArea(A, 0, 2));
32+
}
33+
34+
public int minArea(char[][] image, int x, int y) {
35+
done = new boolean[image.length][image[0].length];
36+
maxR = 0; maxC = 0; minR = Integer.MAX_VALUE; minC = Integer.MAX_VALUE;
37+
maxR = Math.max(maxR, x);
38+
minR = Math.min(minR, x);
39+
40+
maxC = Math.max(maxC, y);
41+
minC = Math.min(minC, y);
42+
dfs(image, x, y);
43+
return ((maxR - minR) + 1) * ((maxC - minC) + 1);
44+
}
45+
46+
private void dfs(char[][] image, int r, int c){
47+
done[r][c] = true;
48+
for(int i = 0; i < 4; i ++){
49+
int newR = r + R[i];
50+
int newC = c + C[i];
51+
if(newR >=0 && newR < image.length && newC >= 0 && newC < image[0].length && !done[newR][newC]){
52+
if(image[newR][newC] == '1'){
53+
maxR = Math.max(maxR, newR);
54+
minR = Math.min(minR, newR);
55+
56+
maxC = Math.max(maxC, newC);
57+
minC = Math.min(minC, newC);
58+
dfs(image, newR, newC);
59+
}
60+
}
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)