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 }
  • 相关阅读:
    Hystrix服务降级
    postman使用教程12-预处理(pre-request) 发送请求
    postman使用教程11- sign 签名预处理(pre-request)
    postman使用教程10-请求前参数预处理(pre-request)
    postman使用教程9-点 code 按钮生成代码段
    postman使用教程8-设置断言(Tests脚本编写)
    postman使用教程7-参数化引用外部文件(txt/csv/json)测试数据
    postman使用教程6-引用随机变量($guid,$timestamp,$randomInt)
    postman使用教程5-Test脚本中自定义变量(参数关联 提取 token 和引用 token )
    postman使用教程4-集合变量(collection variables)的使用
  • 原文地址:https://www.cnblogs.com/lishiblog/p/5776537.html
Copyright © 2011-2022 走看看