session Ⅰ algorithm
problem Ⅰ
542. 01 Matrix
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Example 1:

Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]
Example 2:

Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]
dfs solution wrong
class Solution {
public:
int dfs(vector<vector<int>>& mat, int r, int c, int m, int n){
if(r<0 || r>m-1 || c<0 || c>n-1)return INT_MAX;
if(mat[r][c]==0)return 0;
int up = dfs(mat, r-1, c, m, n)+1;
int down = dfs(mat, r+1, c, m, n)+1;
//int left = dfs(mat, r, c-1, m, n)+1;
//int right = dfs(mat, r, c+1, m, n)+1;
return min(up, down);
//return min(min(up, down), min(left, right));
//return min(min(dfs(mat, r-1,c)+1, dfs(mat, r+1,c)+1), min(dfs(mat, r, c-1)+1, dfs(mat, r, c+1)+1));
}
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
vector<vector<int>> res = vector(m, vector<int>(n, 0));
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(mat[i][j]==0)res[i][j] = 0;
else res[i][j] = dfs(mat, i, j, m, n);
}
}
return res;
}
};
bfs solution
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
vector<vector<int>> res = vector(m, vector<int>(n, INT_MAX));
queue<pair<int, int>> q;
vector<vector<int>> option = {{-1,0},{1,0},{0,-1},{0,1}};
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(mat[i][j]==0){
res[i][j] = 0;
q.push({i, j});
}
}
}
while(!q.empty()){
int curx = q.front().first, cury = q.front().second;
int newx, newy;
q.pop();
for(int i=0; i<4; i++){
newx = curx + option[i][0];
newy = cury + option[i][1];
if(newx >= 0 && newx <= m-1 && newy >= 0 && newy <= n-1){
if(res[newx][newy] > res[curx][cury] + 1){
res[newx][newy] = res[curx][cury] + 1;
q.push({newx, newy});
}
}
}
}
return res;
}
};
NOTE:
my thoughts is a bit like shortest path algorithm, i iterate the matrix at first, and push all element equal to 0 into queue, and set the corresponding position of matrix res to 0 (i open a matrix called res aimed to record the shortest distance to 0 for every element)
and for the element equal to 1, i set the corresponding position of matrix res to INT_MAX
and then we begin to do loop while( queue is not empty)
the enroll process is like that

problem Ⅱ
994. Rotting Oranges
You are given an m x n grid where each cell can have one of three values:
- 0 representing an empty cell,
- 1 representing a fresh orange, or
- 2 representing a rotten orange.
Every minute, any fresh orange that is 4-directionally adjacent to a rotten orange becomes rotten.
Return the minimum number of minutes that must elapse until no cell has a fresh orange. If this is impossible, 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
Explanation: The orange in the bottom left corner (row 2, column 0) is never rotten, because rotting only happens 4-directionally.
Example 3:
Input: grid = [[0,2]]
Output: 0
Explanation: Since there are already no fresh oranges at minute 0, the answer is just 0.
my solution
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int freNum = 0, foulNum = 0, times = -1;
int m = grid.size(), n = grid[0].size();
queue<pair<int, int>> q;
vector<vector<int>> option = {{-1,0},{1,0},{0,-1},{0,1}};
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(grid[i][j]==1)
++freNum;
if(grid[i][j]==2){
++freNum, ++foulNum;
q.push({i, j});
}
}
}
// special judge
if(freNum==0)return 0;
if(foulNum==0)return -1;
while(!q.empty()){
int N = q.size();
times++;
for(int k=0; k<N; k++){
int curx = q.front().first, cury = q.front().second;
q.pop();
for(int i=0; i<4; i++){
int newx = curx + option[i][0], newy = cury + option[i][1];
if(newx >=0 && newx <= m-1 && newy >= 0 && newy <= n-1){
if(grid[newx][newy]==1){
grid[newx][newy] = 2;
foulNum++;
q.push({newx, newy});
}
}
}
}
}
if(freNum == foulNum) return times;
else return -1;
}
};
NOTE:
my thought to iterate through the matrix at first, after that we get the total num of the orange and the num of foul orange, this option’s time complexity is O(M*N)
and then we use BFS to iterate through all orange which is fresh, this is very easy, but we have to use some technique to record the circles we spended to iterate through all orange, the time complexity is O(M+N)
本文介绍了使用深度优先搜索(DFS)和广度优先搜索(BFS)解决LeetCode上的两个问题:01 Matrix求最近0的距离和Rotting Oranges腐烂橙子的最短时间。对于01 Matrix,通过初始化矩阵记录每个元素到最近0的距离;而对于Rotting Oranges,首先获取新鲜和腐烂橙子的数量,然后用BFS遍历所有新鲜橙子,计算腐烂过程所需的最短时间。

420

被折叠的 条评论
为什么被折叠?



