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. 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.
Solution 1 -- DFS
因为题目里说输入里只有一个全是1的联通域。所以这个问题转化为求值为1的点的left most position, right most position, top most position, bottom most position。
可以用DFS,遍历找出所有值为1的点,更新以上四个position value。
Time complexity O(mn)
public class Solution {
private int right;
private int left;
private int top;
private int bottom;
private final int[][] directions = {{0,-1},{0,1},{1,0},{-1,0}};
public int minArea(char[][] image, int x, int y) {
if (image == null || image.length < 1) {
return 0;
}
int m = image.length, n = image[0].length;
bottom = 0;
top = m - 1;
right = 0;
left = n - 1;
boolean[][] visited = new boolean[m][n];
dfs(image, x, y, visited);
int area = 0;
if (top <= bottom && left <= right) {
area = (bottom - top + 1) * (right - left + 1);
}
return area;
}
private void dfs(char[][] image, int x, int y, boolean[][] visited) {
if (x < 0 || x >= image.length || y < 0 || y >= image[0].length) {
return;
}
if (visited[x][y]) {
return;
}
if (image[x][y] != '1') {
return;
}
visited[x][y] = true;
// update left, right, top, bottom
if (x > bottom) {
bottom = x;
}
if (x < top) {
top = x;
}
if (y > right) {
right = y;
}
if (y < left) {
left = y;
}
// dfs
for (int i = 0; i < 4; i++) {
dfs(image, x + directions[i][0], y + directions[i][1], visited);
}
}
}
Solution 2 -- Binary Search
Discuss里给出了二分查找的方法。
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 ...
- 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 ...
- [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 ...
- Kth Smallest Element in a BST 解答
Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...
随机推荐
- js 获取input file路径改变图像地址
html代码 <img id="newImage" alt="100x100" src="__PUBLIC__/img/1.jpg" ...
- LinearLayout 和 RelativeLayout
共有属性: java代码中通过btn1关联次控件 android:id="@+id/btn1" 控件宽度 android:layout_width="80px" ...
- [原创作品] RequireJs入门进阶教程
最近我发现RSS采集数据是个很好玩的东西,就是可以直接把别人的数据放在自己的网站上.如果网友们在其他地方发现这篇文章,还是来博客园看吧(http://zhutty.cnblogs.com).这样代码比 ...
- 解决vsftpd 530 Permission denied报错
虚拟机装好RedHat后,准备使用filezilla连接,输入IP地址,root用户,密码,快速连接,报错: 故障排除: 1.首先检查系统是否开启了vsftp服务,如果没有开启,先开启该服务. 方法1 ...
- compass模块----Utilities----Sprites精灵图合图
css雪碧图又叫css精灵或css sprite,是一种背景图片的拼合技术.使用css雪碧图,能够减少页面的请求数.降低图片占用的字节,以此来达到提升页面访问速度的目的.但是它也有令人诟病的地方,就是 ...
- get请求与post请求
1. get是从服务器上获取数据,post是向服务器传送数据.2. get是把参数数据队列加到提交表单的ACTION属性所指的URL中,值和表单内各个字段一一对应,在URL中可以看到.post是通过H ...
- 为自己的Android应用添加广告
平时也写了不少Android小应用,但是都是做练习之用,从来没有想过添加广告赚钱这一说. 个人是非常反感在应用内添加广告这种行为的,非常影响用户体验,不小心点到广告的话,手机流量哗哗地就没了··· 但 ...
- SQLSERVER内核架构剖析 (转)
我们做管理软件的,主要核心就在数据存储管理上.所以数据库设计是我们的重中之重.为了让我们的管理软件能够稳定.可扩展.性能优秀.可跟踪排错. 可升级部署.可插件运行,我们往往研发自己的管理软件开发平台. ...
- table超过30个字段如何处理呢? bootstrap
样式: @media (max-width: 768px) { .table-supplier { width: 100%; height: 100%; margin-bottom: 12.75px; ...
- CSS凹型导航按钮
一般需求,圆角看起来更加舒服,但是下面直角略显生硬 于是设计师有了下面的需求,下面加上小凹型: 凹型?凹型?凹型?有点变态,这怎么实现........... 图片肯定是最先考虑到的,CSS实现有貌似有 ...