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 ...
随机推荐
- git 查看文件修改记录
今天追了个几年前留下来的坑, 在 git 里追溯修改过程坑死个爹, 具体方法估计没多久又会忘, 还是记下来以后有的参考 大部分教程都会告诉大家使用 git log 来查看对应文件的修改记录, 就像这样 ...
- xm学习笔记
1关于静态网页的制作 html主要负责页面的结构+css页面的美观+js与用户的交互. 2html 有标签体的标签: <p></p> <span></spa ...
- 泛型 Field 和 SetField 方法 (LINQ to DataSet)
LINQ to DataSet 为 DataRow 类提供用于访问列值的扩展方法:Field 方法和 SetField 方法.这些方法使开发人员能够更轻松地访问列值,特别是 null 值.DataSe ...
- EBS OAF开发中的Java 实体对象(Entity Object)验证功能补充
EBS OAF开发中的Java 实体对象(Entity Object)验证功能补充 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) EO理论上 ...
- Oracle—RMAN备份(一)
一.RMAN备份相关概念 1.RMAN备份中表空间不需要处于backup模式下,它备份数据文件,归档日志文件,控制文件,spfile和备份集片,但不备份联机重做日志文件,临时文件和口令文件. 2.备份 ...
- WebApi2官网学习记录---Attribute Routing
从WebApi 1迁移到WebAPI 2要改变配置代码如下: WebApi 1: protected void Application_Start() { // WARNING - Not compa ...
- VC中遍历目标进程中的模块
VC中遍历目标进程中的模块 MFC代码win32 也可以用 在下面代码进行修改转换就可以了CString strModule; 可以换成 char* 但是MODULEENTRY32结构中的szModu ...
- C# Excel 读取为Datatable
最近项目用到的读取Excel 为DataTable 兼容2003.2007.2010.记录一下,以后会用到 引用 NPOI.dll 和 EPPlus.dll using System; using S ...
- OpenGL ES 2.0 纹理映射
纹理坐标用符点数表示,范围一般从0.0到1.0,在纹理坐标系中.纹理坐标系原点在左上侧,向右为S轴,向下为T轴.两个轴的取值范围都是0.0-1.0. 纹理映射 纹理映射:把一幅纹理图应用到相应的几何图 ...
- 使用python发邮件
使用python发邮件 网上有很多发邮件的例子,本人在网上找了一份,稍加修改后使用 上源码 # encoding=utf-8 from email.mime.image import MIMEImag ...