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 = 0, y = 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的更多相关文章
- 【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 ...
- 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 ...
- [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 ...
- 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 ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- [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 ...
- 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. ...
- [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 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- 如何写一个简单的Web Server(一)
在本篇博文中我将介绍如何写一个Web Server.博文中大部分资料我是参考的这篇文章(http://www.linuxhowtos.org/C_C++/socket.htm),英文不错的同学可以 ...
- windows多线程编程(一)(转)
源出处:http://www.cnblogs.com/TenosDoIt/archive/2013/04/15/3022036.html CreateThread:Windows的API函数(SDK函 ...
- Upgrading to Java 8——第三章 Optional and Similar Classes
Java程序员对付空指针异常已经好多年了.在Java8中将有新的方式去处理他们.通过包装一个潜在的可能为null的类称为Optianal. 在Java8中添加了the Optional, Option ...
- 【转】GCC编译使用动态链接库
相关gcc参数:-l -L -shared -fPIC -static -c -o 原文地址:[脚本之家]http://www.jb51.net/article/34990.htm 根据链 ...
- 关于Web服务器域名设置相关知识积累
1.第一个问题,如何将一个服务器映射到一个域名上呢? 在申请域名的时候,会配置服务器IP和域名的对应关系,所以如果系统中只有一个应用的情况下,应用服务器不需要做任何配置. 2.在Tomcat服务 ...
- PKUSC滚粗记
本来想考得这么烂还是别写了,后来想想毕竟是我高中难得的一次经历,靠脑子记的话我这脑残患者将来肯定会忘了啊……QwQ 好像跟我一样用这个题目的神犇都签了一本QwQ Day 0 来的路上我校其他三位OIe ...
- Java多线程——<八>多线程其他概念
一.概述 到第八节,就把多线程基本的概念都说完了.把前面的所有文章加连接在此: Java多线程——<一>概述.定义任务 Java多线程——<二>将任务交给线程,线程声明及启动 ...
- 查看Centos系统信息命令
linux命令行具有强大的功能,我们安装vps后,首先应该知道系统信息,查看这些信息,你会发现Linux命令很简单,你可以按照下面的命令练习. linux系统信息 # uname -a # 查看内核/ ...
- [工作积累] NDK通过Java获取package name 和version
////////////////////////////////////////////////////////////////////////// //Java code snippet //get ...
- <string>和<string.h>的区别
转自:http://blog.csdn.net/houjixin/article/details/8648969 在C++开发过程中经常会遇到两个比较容易混淆的头文件引用#include<str ...