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 ...
随机推荐
- (转)-编写第一个ROS(创建工作空间workspace和功能包package)
原文网址:http://www.cnblogs.com/liuamin/p/5704281.html 刚接触ROS,学着写了第一个程序,怕以后忘记,就将其步骤记录下来.. 首先你必须保证你电脑已安装配 ...
- android 系统应用在运行时被卸载
android 系统应用在运行时被rm 掉了,是否还会运行? 环境:root,且开机运行 经过试验,apk本身是在 /system/app 目录下面,且apk已经运行了,这个时候直接 rm /syst ...
- ReactJS入门
React介绍 React是facebook开发基于组件驱动开发(CDD ) 的UI类库,相对于双向绑定的类库,如AngularJS,它采用单向数据流绑定.通过采用虚拟DOM的概念,是的他在性能和处理 ...
- Dynamic Programming - Part2
实现如下: public static void main(String[] args) { String squence1 = "ABCBDAB"; String squence ...
- NYOJ-85 有趣的数 AC 分类: NYOJ 2014-01-17 21:42 240人阅读 评论(0) 收藏
这道题目就是,找规律,小学奥数,找规律不难吧, #include<stdio.h> int sc(int x); int main(){ int n=0; int num,cs,k; sc ...
- sampleGradient(sampler,uv,dds,ddy)
vsm里面用这个梯度采样 采放了z,z*z的shadowmap 这种采样方式和普通sample有什么区别
- 女性社区TOP10
“女性和孩子的钱是世界上最好赚的”并不是一句空话.据统计,女性掌管着家庭70%的支出,如果你能让女性为你掏出腰包,那么你基本就掌控了一个家庭的大部分的消费. 有趣的是,女性还是一个喜欢分享的群体,他们 ...
- SQL Server 2008中新增的 1.变更数据捕获(CDC) 和 2.更改跟踪
概述 1.变更数据捕获(CDC) 每一次的数据操作都会记录下来 2.更改跟踪 只会记录最新一条记录 以上两种的区别: http://blog.csdn.n ...
- CSS3:线上编辑工具及实用资料整理
an I Use 个人最常用的,资料比较全,桌面和移动浏览器支持HTML5,CSS3,SVG和兼容性表. 官网地址:http://caniuse.com/ CSS3 Click Chart CSS3 ...
- 10 个让人惊讶的 jQuery 插件
说是让人惊讶,你可能会觉得我们没见过世面,但这里提及的一些 jQuery 的插件的确平时比较少见,用的人应该更少. Grid portfolio 使用竖排方式显示条目信息,现在很流行的的内容布局方式. ...