原题

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.

样例:

given the following image:

[

"0010",

"0110",

"0100"

]

and x = 0, y = 2,

Return 6.

解析

求最小覆盖矩形面积

有一个矩阵,由0,1组成,每个1表示一个黑色像素点,

给出第一个黑色像素的位置,求最小的可以覆盖这个矩阵中黑色像素点的矩形大小

思路

一:遍历整个矩阵(缺点,如果矩阵很大,像素点占比很少,则比较浪费)

二:遍历像素点

找出最大最小的x,最大最小的y然后计算矩阵大小

解法

BFS广度有限算法(Queue)

用给出的一个像素点,利用queue,入队第一个像素点,然后入队它上下左右的像素;当队不空时继续遍历,直到所有的像素都遍历一遍

public int minAreaBFS(int[][] image, int x, int y) {
if (image == null || image.length <= 0 || image[0] == null || image[0].length <= 0) {
return 0;
}
int minX = x, minY = y, maxX = x, maxY = y;
Queue<Point> queue = new LinkedList<>();
Point rootP = new Point(x, y);
queue.offer(rootP);
//队不空时就继续
while (!queue.isEmpty()) {
Point p = queue.poll();
minX = Math.min(p.x, minX);
minY = Math.min(p.y, minY);
maxX = Math.max(p.x, maxX);
maxY = Math.max(p.y, maxY);
//判断四周是否有像素,有就入队
check(p.x - 1, p.y, image, queue);
check(p.x + 1, p.y, image, queue);
check(p.x, p.y - 1, image, queue);
check(p.x, p.y + 1, image, queue);
}
return (maxX - minX + 1) * (maxY - minY + 1);
} private void check(int x, int y, int[][] image, Queue queue) {
int maxR = image.length;
int maxC = image[0].length;
Point p = new Point(x, y);
if (x >= 0 && x < maxR && y >= 0 && y < maxC && image[x][y] == 1) {
//遍历过的点置0
image[x][y] = 0;
queue.offer(p);
}
}

DFS深度优先算法(递归)

用给出的第一个点,递归遍历它的上下左右像素,直到遍历完所有的像素点

public int minAreaDFS(int[][] image, int x, int y) {
if (image == null || image.length <= 0 || image[0] == null || image[0].length <= 0) {
return 0;
}
//数组记录最小x,最小y,最大x,最大y
int[] res = {x, y, x, y};
dfs(x, y, image, res);
return (res[2] - res[0] + 1) * (res[3] - res[1] + 1);
} private void dfs(int x, int y, int[][] image, int[] res) {
int maxR = image.length;
int maxC = image[0].length;
//若当前点存在且为像素,才继续,否则返回
if (x >= 0 && x < maxR && y >= 0 && y < maxC && image[x][y] == 1) {
image[x][y] = 0;
res[0] = Math.min(x, res[0]);
res[1] = Math.min(y, res[1]);
res[2] = Math.max(x, res[2]);
res[3] = Math.max(y, res[3]);
dfs(x - 1, y, image, res);
dfs(x + 1, y, image, res);
dfs(x, y - 1, image, res);
dfs(x, y + 1, image, res);
}
}

【leetcode】302.Smallest Rectangle Enclosing Black Pixels的更多相关文章

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

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

  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. LeetCode Smallest Rectangle Enclosing Black Pixels

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

  5. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  6. 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. 【LeetCode】910. Smallest Range II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  9. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

随机推荐

  1. python 类型注解

    函数定义的弊端 python 是动态语言,变量随时可以被赋值,且能赋值为不同类型 python 不是静态编译型语言,变量类型是在运行器决定的 动态语言很灵活,但是这种特性也是弊端 def add(x, ...

  2. 第一个php文件运行

    运行会发现报错,解决参考:写的很详细 http://blog.csdn.net/meegomeego/article/details/36020553

  3. css调用字体 没装微软雅黑,用css写@font-face让其能显示微软雅黑字体

    在设计布局网页时 经常想要用一些比较好看的字体,比如微软雅黑,这个字体在近年来在网页设计中运用越来越平常, 然而所使用的字体也只有自己能看到 到别的机子上 又恢复了原来的宋体神马的. 经过一位高手的提 ...

  4. 【C#设计模式3】工厂方法模式

    一.引言 在简单工厂模式中讲到简单工厂模式的缺点,有一点是——简单工厂模式系统难以扩展,一旦添加新产品就不得不修改简单工厂方法,这样就会造成简单工厂的实现逻辑过于复杂,然而本专题介绍的工厂方法模式可以 ...

  5. 【计算机视觉】detection/region/object proposal 方法综述文章

    目录(?)[-] Papers 大纲 各种OP方法的回顾 Grouping proposal methods Window scoring proposal methods Aliternate pr ...

  6. CMake生成VS2010工程相对路径和绝对路径问题说明

    CMake生成VS2010工程相对路径和绝对路径问题说明 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 主要是使用CMake生成的VS2010的工程,最好不 ...

  7. Andrew Ng机器学习课程17(1)

    Andrew Ng机器学习课程17(1) 声明:引用请注明出处http://blog.csdn.net/lg1259156776/ 说明:主要介绍了强化学习与监督学习的设定上的区别,以及强化学习的框架 ...

  8. Mysql统计每年每个月的数据——详细教程

    Mysql统计每年每个月的数据(前端页面统计图实现) 最终想实现的效果图,在这里就不多废话了,直接上效果图,由于测试数据有几个月是为0的,所以数据图看着会有点怪怪. 接下来是数据库的两个表,这里直接给 ...

  9. 安装Node.js教程

    前期准备 1.Node.js 简介简单的说 Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境. Node ...

  10. 服务器NGINX连接数

    kill -HUP 1900  重启某个进程! 1.查看Web服务器(Nginx Apache)的并发请求数及其TCP连接状态: netstat -n | awk '/^tcp/ {++S[$NF]} ...