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 ...
随机推荐
- SQL DatePart函数使用
DATEPART 返回代表指定日期的指定日期部分的整数. 语法 DATEPART ( datepart ,date ) 参数 datepart 是指定应返回的日期部分的参数.下表列出了 Microso ...
- [Unity] 查找资源
有时候需要通过代码来为对象指定一个资源.可以通过下面的函数来查找资源. /// <summary> /// 查找资源 /// </summary> /// <return ...
- JQUERY 滚动 scroll事件老忘记 标记下
制作笔记 这个scroll事件 老忘记.... 写的太垃圾了 希望有路过的大神指点的吧~ 这个貌似应该写个函数里 调用好些的吧~ 写个类这样的 也方便扩展貌似 不过就是想想 ~ $(windo ...
- eclipse-4.4.2安装Groovy插件(其他版本eclipse可参考)
步骤 : 1.启动eclipse,点击help -> Install New Software... 在弹出的窗口中点击:Add... Groovy插件的地址:http://dist.sprin ...
- github常见操作和常见错误!
本人总结: 1.问题: fatal: Not a git repository (or any of the parent directories) 解决: 本地库还没有创建,请先用git init ...
- Memcached基础知识
主要内容: Memcached基本的工作原理 Memcached的两阶段哈希 Memcached的数据存储方式 Memcached新建Item分配内存过程 Memcached的数据过期方式 Memca ...
- Java字节流:FilterInputStream FilterOutputStream
----------------------------------------------------------------------------------- FilterInputStrea ...
- solr6.1-----mysql 数据导入-查询
此部分一定要细心,lz 中间错了一个细节,调了好长时间(汗).请严格按照步骤操作 新建core 步骤1: 在webapps中solrhome下新建一个文件夹名字叫做collection1(名字不固定, ...
- 工具介绍 - VSCommands
VSCommands 一个Visual Studio的轻量级扩展工具 地址:http://vscommands.squaredinfinity.com/home 1.可以设置自动隐藏显示主菜单栏,设置 ...
- [译]Bundling and Minification
原文:http://www.asp.net/mvc/overview/performance/bundling-and-minification============================ ...