当前位置:网站首页>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
0For empty cells ; - value
1For fresh oranges ; - value
2For 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.lengthn == grid[i].length1 <= m, n <= 10grid[i][j]Only for0、1or2
2) analysis
Breadth first search .
- Traverse
gridWhere 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
边栏推荐
- 考研英语唐叔的语法课笔记
- Sqoop imports tinyint type fields to boolean type
- An error is reported in the initialization metadata of the dolphin scheduler -- it turns out that there is a special symbol in the password. "$“
- . Ren -- the intimate artifact in the field of vertical Recruitment!
- Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
- JDBC tool class jdbcconutil gets the connection to the database
- LeetCode 994、腐烂的橘子
- SIGIR'22 "Microsoft" CTR estimation: using context information to promote feature representation learning
- C migration project record: modify namespace and folder name
- PostgreSQL basic functions
猜你喜欢

Wave field Dao new species end up, how does usdd break the situation and stabilize the currency market?

Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom

JS arrow function user and processing method of converting arrow function into ordinary function
![Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]](/img/1a/669c330e64af8e75f4b05e472d03d3.png)
Azkaban recompile, solve: could not connect to SMTP host: SMTP 163.com, port: 465 [January 10, 2022]

堡垒机、跳板机JumpServer的搭建,以及使用,图文详细
![[stack and queue topics] - sliding window](/img/65/a2ce87f1401d7a28d4cce0cc85175f.png)
[stack and queue topics] - sliding window

Rt-1052 learning notes - GPIO architecture analysis
![[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph](/img/fb/9822cccde4ca39d8066024c09a7349.png)
[graph theory brush question-5] Li Kou 1971 Find out if there is a path in the graph

SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions

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
随机推荐
Solve the Chinese garbled code of URL in JS - decoding
RT-1052学习笔记 - GPIO架构分析
Investigate why close is required after sqlsession is used in mybatties
Linux64Bit下安装MySQL5.6-不能修改root密码
【栈和队列专题】—— 滑动窗口
The R language uses the timeroc package to calculate the multi time AUC value of survival data without competitive risk, and uses the confint function to calculate the confidence interval value of mul
【问题解决】‘ascii‘ codec can‘t encode characters in position xx-xx: ordinal not in range(128)
Intersection calculation of straight line and plane in PCL point cloud processing (53)
Common form verification
Markdown < a > tag new page open link
JS arrow function user and processing method of converting arrow function into ordinary function
一. js的深拷贝和浅拷贝
Error reported by Azkaban: Azkaban jobExecutor. utils. process. ProcessFailureException: Process exited with code 64
Install MySQL 5.0 under Linux 64bit 6 - the root password cannot be modified
How about CICC fortune? Is it safe to open an account
Thirty What are VM and VC?
bounding box iou
Vscode download speed up
LeetCode 232、用栈实现队列
R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of