LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/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.
题解:
利用binary search来找四个边界。
举例找左边第一个'1', 从横向的0到y, 取mid, 若是mid对应的这一列若是包含'1', 说明左边第一个'1'在mid左侧,在mid左侧继续查找.
找右侧第一个'0', 从很像的y+1到image[0].length, 取mid, 若是mid对应的这一列包含'1', 说明右边第一个'0'在mid右侧,在mid右侧继续查找.
右侧第一个'0'的index 减掉 左侧第一个'1'的index 就是横向包含'1'的长度.
上下也是同理.
Time Complexity: O(m*logn + n*logm). Space: O(1).
AC Java:
public class Solution {
public int minArea(char[][] image, int x, int y) {
if(image == null || image.length == 0 || image[0].length == 0){
return 0;
}
int m = image.length;
int n = image[0].length;
if(x<0 || x>=m || y<0 || y>=n){
return 0;
}
int left = binarySearch(image, 0, y, 0, m, true, true); //左边第一个 '1'
int right = binarySearch(image, y+1, n, 0, m, true, false); //右边第一个 '0'
int up = binarySearch(image, 0, x, 0, n, false, true); // 上边第一个 '1'
int down = binarySearch(image, x+1, m, 0, n, false, false); //下边第一个 '0'
return (right-left) * (down - up);
}
private int binarySearch(char [][] image, int low, int high, int min, int max, boolean searchHor, boolean searchFirstOne){
while(low < high){
int mid = low + (high - low)/2;
boolean existBlack = false;
for(int i = min; i<max; i++){
if((searchHor ? image[i][mid] : image[mid][i]) == '1'){
existBlack = true;
break;
}
}
if(existBlack == searchFirstOne){
high = mid;
}else{
low = mid+1;
}
}
return low;
}
}
LeetCode 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】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 ...
- 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 ...
- [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 ...
- 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 ...
- [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. ...
- 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 Maximal Rectangle 单调栈
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052721.html 题目链接:leetcode Maximal Rectangle 单调栈 ...
随机推荐
- 编程之路 - 写给打算进入IT行业的新人们
IT=挨踢,这是IT人的自嘲,进入IT行业是有四五年了,也算得上是一个“老人”了吧,见了不少新人,面试了不少新人,也带了一些新人,多多少少还是有点发言权的. 关于书本 新人们常常会说我看了多少多少的书 ...
- <构建之法>3-5章感想
提示:(下面的总结我会按照每章发现的问题,自己的回答,感想来陈述) 3章. 在阅读3.2.4职业成长-自我评估的时候,说到CRUD需要一些核心技术和许多控扩展的知识,那么作为软件工程的学生,在学校除了 ...
- NOIP 2005 等价表达式 (TYVJ P1060)
做题记录: 2016-08-10 23:35:09 背景 NOIP2005 提高组 第四道 描述 明明进了中学之后,学到了代数表达式.有一天,他碰到一个很麻烦的选择题.这个题目的题干中首先给出了一个代 ...
- HTML5与移动端web学习笔记
HTML5 提供了很多新的功能,主要有: 新的 HTML 元素,例如 section, nav, header, footer, article 等 用于绘画的 Canvas 元素 用于多媒体播放的 ...
- JSP -- for循环按钮处理事件
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- Infragistics公司的UltraWebGrid控件在显示的时候报“theForm” 未定义错误的解决。
在项目中使用了Infragistics公司的UltraWebGrid控件,浏览器中报错,“theForm” 未定义,并且造成客户端js,滚动条,失效.最后查官网论坛找到问题.需要把web.config ...
- 运行java的class文件方法详解
一.运行class文件 执行带main方法的class文件,命令行为:java <CLASS文件名>注意:CLASS文件名不要带文件后缀.class 例如: 复制代码代码如下: java ...
- DockerFile 参数详解
Docker 指令: From --- ENV ---设置环境变量ENV App_DIR /appp Add 和 Copy 可以复制文件到容器里面 .区别 Add 可以写网络的链接地址 Add 支持解 ...
- - dequeueReusableCellWithIdentifier:
与之对应的还有一个方法: - dequeueReusableCellWithIdentifier:forIndexPath: 1 > - dequeueReusableCellWithIdent ...
- Android-----搭建开发环境AND模拟器配置AND启动项目
开发工具我这里用的是eclipse 你也可以用Google最新推出的Android Studio开发工具(不需要配置) 下载地址:https://developer.android.com/sdk/i ...