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

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

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

  3. LeetCode Smallest Rectangle Enclosing Black Pixels

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

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

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

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

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

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

  9. Kth Smallest Element in a BST 解答

    Question Given a binary search tree, write a function kthSmallest to find the kth smallest element i ...

随机推荐

  1. Windows 8.1下使用IE 64位

    Internet Options -> Advanced -> Settings Security组 对Enable 64-bit processes for Enhanced Prote ...

  2. ZOJ3519-Beautiful People:最长上升子序列的变形

    Beautiful People Special JudgeTime Limit: 10000/5000MS (Java/Others)Memory Limit: 128000/64000KB (Ja ...

  3. 如何在 Android 手机上实现抓包?

    如何在 Android 手机上实现抓包? http://www.zhihu.com/question/20467503 我想知道某个应用究竟在数据提交到哪里,提交了什么.网上的教程太复杂,不想麻烦.有 ...

  4. Android TextView中实现点击文本超链接(无下划线)的封装类

    android中有的时候须要在TextView上设置一些超链接,点击这些超链接时进行一些操作.比如新浪微博上的一些keyword,点击时会跳转到对应的页面. 怎样实现我们就直接看源代码吧. /** * ...

  5. install samba on crux without net.

    1. 解压文件 tar -xvfz samba-.tar.gz 2. 进入目录 cd samba- 3. 配置 ./configure 4. 编译 make 5. 安装 make install

  6. [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]无效的可能原因

    开发的用户控件封存在dll中,其他都很顺利,就是这个图片弹出选择路径怎么也搞不出来!(浪费了我半天*2,o(︶︿︶)o 唉,犟脾气拗不过 看了很多搜索信息都说加: [Editor(typeof(Ima ...

  7. css3标签

    -moz代表firefox浏览器私有属性 -ms代表ie浏览器私有属性 -webkit代表chrome.safari私有属性 -o代表opera私有属性 border-radius:2em; 向div ...

  8. call()与apply()传参需要注意的一点

    call()与apply()是用来改变函数体内的this指向,第一个参数是调用函数的母对象,他是调用上下文,函数体内通过this来获得对它的引用,换句话说就是第一参数===函数中的this. 但是如下 ...

  9. OpenGL ES 2.0 雾

    在场景中使用雾不但可以提高真实感,特定的情况下还能优化性能.具体是指当物体离摄像机足够远时,雾就足够浓,此时只能看到雾而看不到物体,也就不必对物体着色进行详细计算,这样可以大大提高渲染效率. 雾有很多 ...

  10. android如何保存读取读取文件文件保存到SDcard

    android如何保存读取读取文件文件保存到SDcard 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图 ...