zoukankan      html  css  js  c++  java
  • 302. 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 = 0y = 2,

    Return 6.

    链接: http://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/

    题解:

    找到包含所有black pixel的最小矩形。这里我们用二分查找。因为给定black pixel点(x,y),并且所有black pixel都是联通的,以row search为例, 所有含有black pixel的column,映射到row x上时,必定是连续的。这样我们可以使用binary search,在0到y里面搜索最左边含有black pixel的一列。接下来可以继续搜索上下和右边界。搜索右边界和下边界的时候,其实我们要找的是第一个'0',所以要传入一个boolean变量searchLo来判断。

    Time Complexity - O(mlogn + nlogm), Space Complexity - O(1)

    public class Solution {
        public int minArea(char[][] image, int x, int y) {
            if(image == null || image.length == 0) {
                return 0;
            }
            int rowNum = image.length, colNum = image[0].length;
            int left = binarySearch(image, 0, y, 0, rowNum, true, true);
            int right = binarySearch(image, y + 1, colNum, 0, rowNum, true, false);
            int top = binarySearch(image, 0, x, left, right, false, true);
            int bot = binarySearch(image, x + 1, rowNum, left, right, false, false);
            
            return (right - left) * (bot - top);
        }
        
        private int binarySearch(char[][] image, int lo, int hi, int min, int max, boolean searchHorizontal, boolean searchLo) {
            while(lo < hi) {
                int mid = lo + (hi - lo) / 2;
                boolean hasBlackPixel = false;
                for(int i = min; i < max; i++) {
                    if((searchHorizontal ? image[i][mid] : image[mid][i]) == '1') {
                        hasBlackPixel = true;
                        break;
                    }
                }
                if(hasBlackPixel == searchLo) {
                    hi = mid;
                } else {
                    lo = mid + 1;
                }
            }
            return lo;
        }
    }

    Reference:

    https://leetcode.com/discuss/68246/c-java-python-binary-search-solution-with-explanation

    https://leetcode.com/discuss/71898/java-concise-binary-search-4x-faster-than-dfs

    https://leetcode.com/discuss/68407/clear-binary-search-python

    https://leetcode.com/discuss/68233/java-dfs-solution-and-seeking-for-a-binary-search-solution

    https://leetcode.com/discuss/68738/very-easy-dfs-java-solution-with-comments

    https://leetcode.com/discuss/70670/dfs-bfs-binary-search-and-brute-force-interation

  • 相关阅读:
    Alcatraz的安装和使用
    TableView didSelectRowAtIndexPath 不执行
    iphone6 plus 为什么打印出的宽度是375
    GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例
    strcmp传入nil导致崩溃
    修改工程名称
    Mac OS X 系统12个常用的文本编辑快捷键(移动、选中)
    删除配置文件解决OS X各种WiFi无法连接的顽固问题,解决MAC无法连接wif的情况 Preferences
    textViewDidChange: crashes in iOS 7
    iOS7隐藏状态栏 statusBar
  • 原文地址:https://www.cnblogs.com/yrbbest/p/5050022.html
Copyright © 2011-2022 走看看