题目:

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 = 0y = 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的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. [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 ...

  4. 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 ...

  5. LeetCode Smallest Rectangle Enclosing Black Pixels

    原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...

  6. [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 ...

  7. 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. ...

  8. [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 ...

  9. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

随机推荐

  1. c 计算 语句 执行 时间

    当然,你也可以用clock函数来计算你的机器运行一个循环或者处理其它事件到底花了多少时间:   #include “stdio.h” #include “stdlib.h” #include “tim ...

  2. 团队项目——特定功能NABC

    我们要做的项目是截屏软件,目前决定做电脑端的应用 我觉得这个软件应该具有随意截屏的功能,就是可以用鼠标拖动线条,只要形成闭合图形就可以将线条内的图像截取出来: NABC模型: N(Need): 许多人 ...

  3. javascript权威指南第六版学习

    第二章 语法结构 2.1 字符集 什么是字符集?各种字符集什么关系?unicode,utf-8是什么关系? 字符(Character)是各种文字和符号的总称,包括各国家文字.标点符号.图形符号.数字等 ...

  4. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  5. MySQL 字符串截取相关函数

    MySQL 字符串截取相关函数 在工作中,可能需要将某些字段按某个分割符组成一个字符串作为字段值存取到数据库表中,比如某个任务对应三个结果,分别存储在不同的数据表中,这时可以将这三个不同表的主键按照约 ...

  6. 关于myeclipse代码提示的一些问题

    默认是  .xxx  输入点提示,要写注释 @xxx的时候怎么输入@后面有代码提示呢? Auto activation delay 是代码提示出现的速度  下面一行是出现代码提示的条件 我们在.后面加 ...

  7. 委托、事件和Lambda

    一.委托 delegate1.在.Net平台下,委托类型用来定义和响应应用程序中的回调.事实上,.Net委托类型是一个类型安全的对象,指向可以以后调用的其他方法,.Net委托是内置支持多路广播和异步方 ...

  8. SSH无密码验证

    一.安装和启动SSH协议 sudo yum install ssh sudo yum install rsync service sshd restart 启动服务 (rsync是一个远程数据同步工具 ...

  9. Sqli-labs less 43

    Less-43 本关与42关的原理基本一致,我们还是定位在login.php中的password.看一下sql语句为: $sql = "SELECT * FROM users WHERE u ...

  10. Python日志输出——logging模块

    Python日志输出——logging模块 标签: loggingpythonimportmodulelog4j 2012-03-06 00:18 31605人阅读 评论(8) 收藏 举报 分类: P ...