-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy path01-matrix.cpp
More file actions
29 lines (24 loc) · 1.04 KB
/
01-matrix.cpp
File metadata and controls
29 lines (24 loc) · 1.04 KB
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
26
27
28
29
class Solution {
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& mat) {
int rows = mat.size(), cols = mat[0].size();
vector <vector<int>> distMat(rows, vector <int> (cols, INT_MAX - 100000));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
if(mat[i][j] == 0) {
distMat[i][j] = 0;
continue;
}
distMat[i][j] = !(i > 0) ? distMat[i][j] : min(distMat[i][j], distMat[i-1][j] + 1);
distMat[i][j] = !(j > 0) ? distMat[i][j] : min(distMat[i][j], distMat[i][j-1] + 1);
}
}
for(int i = rows - 1; i >= 0; i--) {
for(int j = cols - 1; j >= 0; j--) {
distMat[i][j] = !(i < rows - 1) ? distMat[i][j] : min(distMat[i][j], distMat[i+1][j] + 1);
distMat[i][j] = !(j < cols - 1) ? distMat[i][j] : min(distMat[i][j], distMat[i][j+1] + 1);
}
}
return distMat;
}
};