当前位置:网站首页>302. minimum rectangular BFS with all black pixels
302. minimum rectangular BFS with all black pixels
2022-06-26 06:07:00 【Empress Yu】
302. The smallest rectangle containing all black pixels
Pictures are often represented by two-dimensional matrix in computer processing .
Give you a size of m x n The binary matrix of image Represents a black and white picture ,0 Represents white pixels ,1 Represents black pixels .
Black pixels are connected to each other , in other words , There will only be a piece of black pixels connected together in the picture . Pixels are connected horizontally or vertically .
Here are two integers x and y Indicates the position of a black pixel , Please find out the smallest rectangle containing all black pixels ( Align with axis ), And returns the area of the rectangle .
You must design and implement a system with less time complexity than O(mn) Algorithm to solve this problem .
Example 1:
Input :image = [["0","0","1","0"],["0","1","1","0"],["0","1","0","0"]], x = 0, y = 2
Output :6
Example 2:Input :image = [["1"]], x = 0, y = 0
Output :1
Tips :
m == image.length
n == image[i].length
1 <= m, n <= 100
image[i][j] by '0' or '1'
1 <= x < m
1 <= y < n
image[x][y] == '1'
image The black pixels in form only one Componentssource : Power button (LeetCode)
link :https://leetcode.cn/problems/smallest-rectangle-enclosing-black-pixels
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
The result of doing the question
success , This question is very watery , At most mid?
Method :BFS
1. Extend from a given black area point
2. Take the maximum 、 The smallest x and y
3. The actual width is obtained by subtracting from the coordinate difference , Multiply to get the smallest rectangle
class Solution {
public int minArea(char[][] image, int x, int y) {
int m = image.length;
int n = image[0].length;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{x,y});
int[] directions = new int[]{0,1,0,-1,0};
int minX = x;
int maxX = x;
int minY = y;
int maxY = y;
image[x][y] = 0;
while(!queue.isEmpty()){
int[] item = queue.poll();
for(int i = 0; i < 4; i++){
int x1 = item[0]+directions[i];
int y1 = item[1]+directions[i+1];
if(x1>=0&&x1<m&&y1>=0&&y1<n&&image[x1][y1]=='1'){
minX = Math.min(minX,x1);
maxX = Math.max(maxX,x1);
minY = Math.min(minY,y1);
maxY = Math.max(maxY,y1);
image[x1][y1] = 0;
queue.offer(new int[]{x1,y1});
}
}
}
return (maxX-minX+1)*(maxY-minY+1);
}
}边栏推荐
- Class and object learning
- 小程序第三方微信授权登录的实现
- Solve the problem that Cmdr cannot use find command under win10
- 5 minutes to learn regular expressions
- Test depends on abstraction and does not depend on concrete
- Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
- 04. basic data type - list, tuple
- 怎么把平板作为电脑的第二扩展屏幕
- Consul service registration and discovery
- 家庭记账程序(第二版 加入了循环)
猜你喜欢

【群内问题学期汇总】初学者的部分参考问题

Tortoise and rabbit race example

【C語言】深度剖析數據在內存中的存儲

MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications

Deeply uncover Ali (ant financial) technical interview process with preliminary preparation and learning direction

How to use the tablet as the second extended screen of the PC
Explore small program audio and video calls and interactive live broadcast from New Oriental live broadcast

Redis多线程与ACL

Combined mode, transparent mode and secure mode

Redis multithreading and ACL
随机推荐
Overloading and overriding
机器学习 05:非线性支持向量机
原型模式,咩咩乱叫
Tencent's 2022 school recruitment of large factories started with salary, and the general contracting of cabbage is close to 40W!
How to associate wechat applet QR code to realize two code aggregation
Redis multithreading and ACL
Prototype mode, Baa Baa
"= =" difference from "equals"
Level signal and differential signal
工作积累——Web请求中使用ThreadLocal遇见的问题
SQL query time period content
Matching environment of ES6
SQL server functions
Sql语法中循环的使用
工厂方法模式、抽象工厂模式
小程序如何关联微信小程序二维码,实现二码聚合
Mysql-10 (key)
Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
numpy. random. choice
REUSE_ ALV_ GRID_ Display event implementation (data_changed)