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.
这题既可以用DFS,也可以二分查找,因为题目中明确说了,只有一块黑色区域。
代码来自:http://www.cnblogs.com/yrbbest/p/5050022.html
public class Solution {
public int minArea(char[][] image, int x, int y) {
if(image == null || image.length == ) {
return ;
}
int rowNum = image.length, colNum = image[].length;
int left = binarySearch(image, , y, , rowNum, true, true);
int right = binarySearch(image, y + , colNum, , rowNum, true, false);
int top = binarySearch(image, , x, left, right, false, true);
int bot = binarySearch(image, x + , 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) / ;
boolean hasBlackPixel = false;
for(int i = min; i < max; i++) {
if((searchHorizontal ? image[i][mid] : image[mid][i]) == '') {
hasBlackPixel = true;
break;
}
}
if(hasBlackPixel == searchLo) {
hi = mid;
} else {
lo = mid + ;
}
}
return lo;
}
}
Smallest Rectangle Enclosing Black Pixels的更多相关文章
- [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 ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- 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 b ...
- [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】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 All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
随机推荐
- rpm 命令
这些事rpm的常用参数!!! 你可以在linux下man 一下rpm就知道了!!! 不过是英文的,不然你可以百度一下rpm就知道了额!!! 下面我帮你贴几个!!!!rpm 常用命令1.安装一个包 # ...
- apache 的工作模式
总结:访问量大的时候使用 worker模式: 每个进程,启动多个线程来处理请求,每个线程处理一次请求,对内存要求比较高. prefoek模式 : 每个子进程只有一个线程,一次请求一个进程. 什么是a ...
- 【转】flume+kafka+zookeeper 日志收集平台的搭建
from:https://my.oschina.net/jastme/blog/600573 flume+kafka+zookeeper 日志收集平台的搭建 收藏 jastme 发表于 10个月前 阅 ...
- winScp上传文件时,如何过滤制定文件
在用winScp上传文件时,有些文件不想上传到服务器上.怎么办呢? 比如我希望过滤.svn .git文件和目录怎么操作呢? 第一步:在菜单上选中选项,点击选项. 第二步,点击传输->编辑 第三步 ...
- jQuery源码-dom操作之jQuery.fn.html
写在前面 前面陆陆续续写了jQuery源码的一些分析,尽可能地想要cover里面的源码细节,结果导致进度有些缓慢.jQuery的源码本来就比较晦涩,里面还有很多为了解决兼容问题很引入的神代码,如果不g ...
- Excel 使用宏批量修改单元格内指定文字为红字
-> step 1:新建宏,进入编辑,使用如下代码: Sub Ss()Dim c As RangeFor Each c In ActiveSheet.UsedRange i = 1 While ...
- 如何在R中加载”xlsx”包
1.下载安装对应系统位数的JDK包(Java SE Development Kit) 2.完成后,安装rJava包-low-level r to Java Interface install.pack ...
- springmvc之DispatcherServlet
1.作用:DispatcherServlet是前置控制器,配置在web.xml(因为DispatcherServlet是一个servelet)文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已 ...
- WordPress文章浏览历史插件
选自:http://www.ludou.org/wordpress-recently-viewed.html 最近有很多网友问我,露兜博客右边栏底部的 您刚刚看过 栏目是怎么实现.其实我也是参考的这篇 ...
- Java 的printf(转)
出处:http://blog.csdn.net/swandragon/article/details/4653600 public class TestPrintf{public static voi ...