当前位置:网站首页>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
边栏推荐
- R language uses econocrats package to create microeconomic or macroeconomic map, visualize indifference function indifference curve, customize calculation intersection, and customize the parameters of
- Matlab analytic hierarchy process to quickly calculate the weight
- Numpy - creation of data type and array
- The construction and use of Fortress machine and springboard machine jumpserver are detailed in pictures and texts
- PCL点云处理之基于PCA的几何形状特征计算(五十二)
- Solution to PowerDesigner's failure to connect to MySQL in x64 system
- PCL点云处理之直线与平面的交点计算(五十三)
- Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
- Servlet learning notes
- PIP installation package reports an error. Could not find a version that satisfies the requirement pymysql (from versions: none)
猜你喜欢
[PTA] get rid of singles
C migration project record: modify namespace and folder name
SQL Server Connectors By Thread Pool | DTSQLServerTP plugin instructions
Don't bother tensorflow learning notes (10-12) -- Constructing a simple neural network and its visualization
内网渗透之DOS命令
DNS cloud school rising posture! Three advanced uses of authoritative DNS
Zdns was invited to attend the annual conference of Tencent cloud basic resources and share the 2020 domain name industry development report
Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
After route link navigation, the sub page does not display the navigation style problem
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
随机推荐
上海回應“面粉官網是非法網站”:疏於運維被“黑”,警方已立案
C migration project record: modify namespace and folder name
Fundamentals of network communication (LAN, Wan, IP address, port number, protocol, encapsulation and distribution)
Notes of Tang Shu's grammar class in postgraduate entrance examination English
Es error: request contains unrecognized parameter [ignore_throttled]
Es keyword sorting error reason = fielddata is disabled on text fields by default Set fielddata = true on keyword in order
Cadence OrCAD capture batch change component packaging function introduction graphic tutorial and video demonstration
Devaxpress report replay: complete the drawing of conventional two-dimensional report + histogram + pie chart
Numpy sort search count set
Es index (document name) fuzzy query method (database name fuzzy query method)
Mysql database backup scheme
Imitation Baidu map realizes the three buttons to switch the map mode by automatically shrinking the bottom
DNS cloud school | analysis of hidden tunnel attacks in the hidden corner of DNS
Handwritten Google's first generation distributed computing framework MapReduce
Rédaction de thèses 19: différences entre les thèses de conférence et les thèses périodiques
Still using listview? Use animatedlist to make list elements move
BMP JPEG picture to vector image contourtrace
16MySQL之DCL 中 COMMIT和ROllBACK
Modeling based on catiav6
BMP JPEG 图片转换为矢量图像 ContourTrace