题目:

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

302. Smallest Rectangle Enclosing Black Pixels的更多相关文章

  1. 【leetcode】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 bl ...

  2. LC 302. Smallest Rectangle Enclosing Black Pixels【lock, hard】

    An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. The black ...

  3. [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 ...

  4. 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 ...

  5. LeetCode Smallest Rectangle Enclosing Black Pixels

    原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...

  6. [Locked] 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 ...

  7. Smallest Rectangle Enclosing Black Pixels 解答

    Question An image is represented by a binary matrix with 0 as a white pixel and 1 as a black pixel. ...

  8. [Swift]LeetCode302. 包含黑色像素的最小矩形 $ 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 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. Log4Net学习【三】

    Log4Net配置详解 配置方式一 在相应的应用程序的配置文件中配置,(WinForm对应的是*.exe.config,WebForm对应的是*.config),本实例是Web应用程序,以Web.co ...

  2. C#和asp.net执行外部EXE程序

    这两天研究下.Net的执行外部EXE程序问题,就是在一个程序里通过按钮或其他操作运行起来另外一个程序,需要传入参数,如用户名.密码之类(实际上很类似单点登录,不过要简单的多的多):总结如下: 1.CS ...

  3. 12、android socket使用demo:网络聊天

    目录: 一.效果图 二.原代码分享 三.代码分析 四.总结 一.效果图如下: 客户端1: 客户端2:           二.原代码分享如下: 1.java代码只有一个 MainActivity.ja ...

  4. Netsharp快速入门(之16) Netsharp基础功能(权限管理)

    第5章     Netsharp基础功能 5.1     权限配置 5.1.1  功能权限 1.配置权限功能点,打开平台工具-基础业务-操作管理 2.选择资源节点为销售订单,点添加常用操作,添加完成后 ...

  5. selenium使用IE 浏览器问题

    代码如下: #coding=utf-8 from selenium import webdriver driver=webdriver.Ie() driver.get('https://www.bai ...

  6. python 关键字参数

    原文地址:http://docs.pythontab.com/python/python3.4/controlflow.html#tut-functions 函数可以通过 关键字参数 的形式来调用,形 ...

  7. python 实现斐波那契数列

    def fib(n): a,b=0,1 while a<n: print(a,end=" ") a,b=b,a+b print() fib(2000) 输出: 0 1 1 2 ...

  8. 查看python selenium的api

    打开命令行工具,doc中输入: python -m pydoc -p 然后在浏览器中访问http://localhost:4567/,此时应该可以看到python中所有的Modules 按ctrl+f ...

  9. [百度空间] ld: add library file reference by path & file name

    By default, -l option will search libraries with lib* prefix in speficied search paths. i.e. 1 ld -o ...

  10. C/C++中内存区域划分大总结

    C++作为一款C语言的升级版本,具有非常强大的功能.它不但能够支持各种程序设计风格,而且还具有C语言的所有功能.我们在这里为大家介绍的是其中一个比较重要的内容,C和C++内存区域的划分. 一. 在c中 ...