zoukankan      html  css  js  c++  java
  • LeetCode-Smallest Rectangle Enclosing Black Pixels

    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 }
  • 相关阅读:
    【Java基础】9、Enumeration接口和Iterator接口的区别
    【Java基础】4、java中的内部类
    【Java基础】3、Java 位运算(移位、位与、或、异或、非)
    【高并发解决方案】6、数据库水平切分的实现原理解析
    【Java深入研究】6、CGLib动态代理机制详解
    【Java深入研究】4、fail-fast机制
    【Java深入研究】2、JDK 1.8 LinkedList源码解析
    【算法】1、快速排序
    Linux下查看系统版本号信息的方法
    Kubernetes1.2如何使用iptables
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5776537.html
Copyright © 2011-2022 走看看