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 单调栈 ...
随机推荐
- iOS学习03C语言循环结构
1.while循环 while (循环条件) { 循环体: } // 1.定义循环变量 ; // 2.循环条件 ) { // 3.循环体 printf("%d\n", ...
- Leetcode Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- 【POJ】1160 Post Office
http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...
- Codeforces Beta Round #8
A题,小小的模拟题,没看懂题意啊. #include <iostream> #include <cstdio> #include <cmath> #include ...
- UESTC 1307 windy数(数位DP)
题目链接 这其实入门题,不过,我写了好一会,还是一直wa,图样... #include <iostream> #include <cstdio> #include <cs ...
- 应对Memcached缓存失效,导致高并发查询DB的四种思路(l转)
当Memcached缓存失效时,容易出现高并发的查询DB,导致DB压力骤然上升. 这篇blog主要是探讨如何在缓存将要失效时,及时地更新缓存,而不是如何在缓存失效之后,如何防止高并发的DB查询. 解决 ...
- GO语言练习:网络编程 ICMP 示例
1.代码 2.编译及运行 1.Go语言网络编程:ICMP示例代码 icmptest.go package main import ( "fmt" "net" & ...
- java中instanceof和getClass()的区别分析
class A { } class B extends A { } Object o1 = new A(); Object o2 = new B(); o1 instanceof A => t ...
- EasyUI组件(窗口组件)
注意首先要在title后面导入配置文件,前后顺序不能乱 <!-- 1.jQuery的js包 --><script type="text/javascript" s ...
- CSS样式表 选择器
1.内联样式表 和HTML联合显示,控制精确,但是可重用性差,冗余较多. 例:<p style="font-size:14px;">内联样式表</p> &l ...