-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-matrix.cpp
25 lines (24 loc) · 858 Bytes
/
01-matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// https://leetcode.com/problems/01-matrix/
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int m = mat.size();
int n = mat[0].size();
for(int i = 0; i<m; i++){
for(int j = 0; j<n; j++){
if(mat[i][j] == 0) continue;
mat[i][j] = 100000001;
if(i > 0) mat[i][j] = min(mat[i][j], mat[i - 1][j] + 1);
if(j > 0) mat[i][j] = min(mat[i][j], mat[i][j - 1] + 1);
}
}
for(int i = m - 1; i >= 0; i--){
for(int j = n - 1; j >= 0; j--){
if(mat[i][j] == 0) continue;
if(i < m - 1) mat[i][j] = min(mat[i][j], mat[i + 1][j] + 1);
if(j < n - 1) mat[i][j] = min(mat[i][j], mat[i][j + 1] + 1);
}
}
return mat;
}
};