【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 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的更多相关文章
- 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 ...
- 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 ...
- [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 ...
- LeetCode Smallest Rectangle Enclosing Black Pixels
原题链接在这里:https://leetcode.com/problems/smallest-rectangle-enclosing-black-pixels/ 题目: An image is rep ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】1022. Smallest Integer Divisible by K 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】988. Smallest String Starting From Leaf 解题报告(C++ & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...
- 【LeetCode】910. Smallest Range II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
随机推荐
- hive-1.1.0-cdh5.11.1-src compile
1. download cdh hive src http://archive.cloudera.com/cdh5/cdh/5/hive-1.1.0-cdh5.11.1-src.tar.gz 2. ...
- MySQL创建触发器样例
# init DROP TABLE IF EXISTS students; DROP TABLE IF EXISTS class; # 创建测试用的班级表 CREATE TABLE class ( c ...
- 移动App书写Test Case时需要考虑的检查点
在测试工作中我们需要不断的总结和储备自己的知识和经验,譬如具备特定属性.环境以及场景,如:PC,手机,智能设备,特定网络环境下. 我们需要关注的功能点,容易出错的位置,这将对我们整个测试过程起至关作用 ...
- iOS-UIScreen,UIFont,UIColor,UIView,UIButton
6.1 UIScreen // 屏幕的宽度 CGFloat screenW = [UIScreen mainScreen].bounds.size.width; 6.2 UIFont + (UIFon ...
- Xena L23网络测试仪Valkyrie使用技巧100例:使用Xena官方在线演示设备 (编号00)
需求# 1.新用户:没有硬件,想看看软件长什么样,好不好用,风格如何,怎么办? 2.代理商:没有硬件,想给客户Show一下Xena高大上的软件,怎么办? 3.老用户:邮件推送了新的软件版本,据说很多新 ...
- 乐字节Java反射之一:反射概念与获取反射源头class
一.Java反射机制概念 “程序运行时,允许改变程序结构或变量类型,这种语言称为动态语言”,如Python, Ruby是动态语言:显然C++,Java,C#不是动态语言,但是JAVA有着一个非常突出 ...
- 记crt 在windows与linux服务器之间利用ftp进行文件的上传下载
我们首先来熟悉一些相关的命令以及操作: ls #展示linux服务器当前工作目录[你连接sftp时所处的目录]下的所有文件以及文件夹 lcd F:\niki #绑定你在windo ...
- Docker部署Gitlab11.10.4
1.下载镜像 官方镜像地址:https://hub.docker.com/r/gitlab/gitlab-ce ,根据自己需要下载指定版本 [root@vanje-dev01 ~]# docker p ...
- java学习基础知识入门
基础入门知识(一) 一.java技术的分类 java按照技术标准和应用场景的不同分为三类,分别是JAVASE.JAVAEE.JAVAME JAVASE : 平台标准版,用于开发部署桌面,服务器以及嵌入 ...
- Java 最常见 200+ 面试题 + 全解析
本文分为十九个模块,分别是: Java 基础.容器.多线程.反射.对象拷贝.Java Web .异常.网络.设计模式.Spring/Spring MVC.Spring Boot/Spring Clou ...