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 ...
随机推荐
- 模仿TMALL搜索,下拉提示 优化 用户keypress停顿200毫秒间隔时,在执行异步取数据操作 通过underscore的函数debounce来实现
- qq2013 java版(完整工程源码 包含服务端 oracle数据库)毕业设计有用
/** * 初始化组件 */ private void initComponent() { //提示面板 errorTipPane = new ErrorTipPane(); // 主面板 mainP ...
- 关于bootstrap--表单(水平表单)
在Bootstrap框架中要实现水平表单效果,必须满足以下两个条件:1.在<form>元素是使用类名“form-horizontal”.2.配合Bootstrap框架的网格系统.(网格布局 ...
- PHP常用魔术方法(__call魔术方法:)
魔术方法 __call <?php //文件名:index.php define('a',__DIR__); include '/IMooc/Loader.php'; spl_autoload ...
- http2.0 相对于 http1.1的优势
1.http2.0完全是多路复用的,只需一个连接就可实现并行 可以将不同的请求夹杂在一起,只需一个连接就能加载一个页面. 2.可以让服务器将响应主动推动到客户端缓存中 当浏览器请求一个网页时,服务器除 ...
- each,map,grep的区别
var arr = ["aa","bb","{name:apple}"]; 1.each的使用 var a = $.each(arr,fun ...
- WebService-使用JDK开发WebService
一.使用JDK开发WebService 2.1.开发WebService服务器端 1.定义一个interface,使用@WebService注解标注接口,使用@WebMethod注解标注接口中定义的所 ...
- spring-data-mongodb必须了解的操作
http://docs.spring.io/spring-data/data-mongo/docs/1.0.0.M5/api/org/springframework/data/mongodb/core ...
- Understanding Abstractions of Secure Channels 的研读
- 卸载rpm包提示:error: specifies multiple packages
–allmatches Remove all versions of the package which match PACKAGE_NAME. Normally an error is issue ...