LeetCode-Smallest Rectangle Enclosing Black Pixels

LiBlog發表於2016-08-16

An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black pixels are connected, i.e., there is only one black region. Pixels are connected horizontally and vertically. Given the location (x, y) of one of the black pixels, return the area of the smallest (axis-aligned) rectangle that encloses all black pixels.

For example, given the following image:

[
  "0010",
  "0110",
  "0100"
]

and x = 0, y = 2,

Return 6.

Analysis:

1. Using bfs, regular soltion.

2. Using binary search: the left most col with 1, the right most col with 1, the up most row with 1, the low most row with 1. Much faster.

Solution 1:

 1 public class Solution {
 2     int[][] moves = new int[][] {{1,0},{-1,0},{0,1},{0,-1}};
 3     public int minArea(char[][] image, int x, int y) {
 4         if (image.length==0 || image[0].length==0) return 0;
 5         
 6         int rows = image.length, cols = image[0].length;
 7         int up = x, low = x, left = y, right = y;
 8         Queue<int[]> pixels = new LinkedList<int[]>();
 9         pixels.add(new int[]{x,y});
10         
11         while (!pixels.isEmpty()){
12             // Get head pixel
13             int[] head = pixels.poll();
14 
15             // Add its neighbors into queue. 
16             for (int i=0;i<4;i++){
17                 int[] neighbor = new int[]{head[0]+moves[i][0],head[1]+moves[i][1]};
18                 if (neighbor[0]>=0 && neighbor[0]<rows && neighbor[1]>=0 && neighbor[1]<cols && image[neighbor[0]][neighbor[1]] == '1'){
19                     pixels.add(neighbor);
20                     image[neighbor[0]][neighbor[1]] = '2';
21                     // update bounds
22                     up = Math.min(up,neighbor[0]);
23                     low = Math.max(low,neighbor[0]);
24                     left = Math.min(left,neighbor[1]);
25                     right = Math.max(right,neighbor[1]);
26                 }
27             }
28         }
29 
30         // Calculate area
31         int res = (low-up+1) * (right-left+1);
32         return res;
33     }
34 
35 
36 }

 

Solution 2:

 1 public class Solution {
 2     public int minArea(char[][] image, int x, int y) {
 3         if (image.length==0 || image[0].length==0) return 0;
 4 
 5         int left = searchBoundary(image,0,y-1,true,true);
 6         int right = searchBoundary(image,y+1,image[0].length-1,true,false);
 7         int up = searchBoundary(image,0,x-1,false,true);
 8         int low = searchBoundary(image,x+1,image.length-1,false,false);
 9 
10         return (right-left+1) * (low-up+1);
11     }
12 
13     public int searchBoundary(char[][] image, int start, int end, boolean horizon, boolean lowerDirect){
14         int iterBound = (horizon) ? image.length : image[0].length;
15         while (start<=end){
16             int mid = start + (end - start)/2;
17             boolean hasOne = false;
18             for (int i=0;i<iterBound;i++){
19                 char target = (horizon) ? image[i][mid] : image[mid][i];
20                 if (target=='1'){
21                     hasOne = true;
22                     break;
23                 }
24             }
25 
26             if ( (hasOne && lowerDirect) || (!hasOne && !lowerDirect)){
27                 end = mid-1;
28             } else {
29                 start = mid+1;
30             }
31         }
32         return (lowerDirect) ? start : start-1;            
33     }
34 }

 

相關文章