当前位置:网站首页>Leetcode 994, rotten orange
Leetcode 994, rotten orange
2022-04-23 20:26:00 【Die in a trap】
994、 Rotten oranges
1) Title Description
In the given m x n
grid grid
in , Each cell can have one of three values :
- value
0
For empty cells ; - value
1
For fresh oranges ; - value
2
For rotten oranges .
Every minute , Rotten oranges Around 4 Adjacent in two directions All fresh oranges rot .
return The minimum number of minutes that must elapse until there are no fresh oranges in the cell . If not , return -1
.
Example 1:
Input :grid = [[2,1,1],[1,1,0],[0,1,1]]
Output :4
Example 2:
Input :grid = [[2,1,1],[0,1,1],[1,0,1]]
Output :-1
explain : Orange in the lower left corner ( The first 2 That's ok , The first 0 Column ) Never rot , Because decay happens only in 4 Positive upward .
Example 3:
Input :grid = [[0,2]]
Output :0
explain : because 0 There are no fresh oranges by minute , So the answer is 0 .
Tips :
m == grid.length
n == grid[i].length
1 <= m, n <= 10
grid[i][j]
Only for0
、1
or2
2) analysis
Breadth first search .
- Traverse
grid
Where to store all rotten oranges , The team , And count the number of fresh oranges ; - Then traverse the queue , If there are fresh oranges around , Then set the time , Then add it to the queue , Reduce the number of fresh oranges by one ;
- After breadth first search , If the number of fresh oranges is not zero , return
-1
.
3)C++
Code
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int m=grid.size();
int n=grid[0].size();
int dx[4]={
1,0,-1,0};
int dy[4]={
0,1,0,-1};
int cnt_1=0;
int res=0;
queue<pair<int,int>> que;
vector<vector<int>> timeCosted(m,vector<int> (n,0));
// Traversing the grid , Record the coordinates of all rotten oranges and the number of fresh oranges
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(grid[i][j]==2)
que.emplace(i,j);
else if(grid[i][j]==1)
cnt_1++;
}
}
// Breadth first search
while(!que.empty()){
int x=que.front().first;
int y=que.front().second;
que.pop();
for(int i=0;i<4;i++){
int newX=x+dx[i];
int newY=y+dy[i];
if(newX>=0&&newX<m&&newY>=0&&newY<n&&grid[newX][newY]==1&&!timeCosted[newX][newY]){
timeCosted[newX][newY]=timeCosted[x][y]+1;
que.emplace(newX,newY);
if(grid[newX][newY]==1){
res=res>timeCosted[newX][newY]?res:timeCosted[newX][newY];
cnt_1-=1;
if(!cnt_1)
break;
}
}
}
}
return cnt_1?-1:res;
}
};
版权声明
本文为[Die in a trap]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232023107507.html
边栏推荐
- Cadence Orcad Capture CIS更换元器件之Link Database 功能介绍图文教程及视频演示
- 内网渗透之DOS命令
- Mathematical modeling column | Part 5: MATLAB optimization model solving method (Part I): Standard Model
- RT-1052学习笔记 - GPIO架构分析
- Cadence Orcad Capture 批量更改元件封装功能介绍图文教程及视频演示
- The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
- How about CICC fortune? Is it safe to open an account
- Actual measurement of automatic ticket grabbing script of barley network based on selenium (the first part of the new year)
- Computing the intersection of two planes in PCL point cloud processing (51)
- [latex] 5 how to quickly write out the latex formula corresponding to the formula
猜你喜欢
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
16MySQL之DCL 中 COMMIT和ROllBACK
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 127
[stack and queue topics] - sliding window
【PTA】L1-002 打印沙漏
Computing the intersection of two planes in PCL point cloud processing (51)
LeetCode 994、腐烂的橘子
Modeling based on catiav6
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
How can matlab obtain the truncated image in trainingimagelabeler
随机推荐
16MySQL之DCL 中 COMMIT和ROllBACK
Vscode download speed up
Historical track data reading of Holux m1200-e Bluetooth GPS track recorder
16MySQL之DCL 中 COMMIT和ROllBACK
PCL点云处理之直线与平面的交点计算(五十三)
WordPress plug-in: WP CHINA Yes solution to slow domestic access to the official website
Research on open source OCR engine
Recognition of high-speed road signs by Matlab using alexnet
Solve the Chinese garbled code of URL in JS - decoding
SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
Shanghai a répondu que « le site officiel de la farine est illégal »: l'exploitation et l'entretien négligents ont été « noirs » et la police a déposé une plainte
Some basic knowledge of devexpress report development
How do BIM swindlers cheat? (turn)
SQL Server connectors by thread pool 𞓜 instructions for dtsqlservertp plug-in
Linux64Bit下安装MySQL5.6-不能修改root密码
Solution to PowerDesigner's failure to connect to MySQL in x64 system
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph
上海回应“面粉官网是非法网站”:疏于运维被“黑”,警方已立案
Computing the intersection of two planes in PCL point cloud processing (51)
DNS cloud school | quickly locate DNS resolution exceptions and keep these four DNS status codes in mind