-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix01.java
More file actions
71 lines (65 loc) · 1.73 KB
/
Copy pathMatrix01.java
File metadata and controls
71 lines (65 loc) · 1.73 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/***
* 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]]
*
*
* Constraints:
*
* m == mat.length
* n == mat[i].length
* 1 <= m, n <= 104
* 1 <= m * n <= 104
* mat[i][j] is either 0 or 1.
* There is at least one 0 in mat.
*/
class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] ans = new int[m][n];
for(int i =0 ; i < m; i++){
for(int j = 0; j < n; j++) {
if(mat[i][j] != 0) ans[i][j] = m * n + 1;
}
}
bfs(ans, mat);
return ans;
}
private void bfs(int[][] ans, int[][] mat){
int m = mat.length, n = mat[0].length;
int maxval = m * n;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(mat[i][j] != 0){
ans[i][j] = Math.min(ans[i][j], Math.min(
i > 0 ? 1 + ans[i - 1][j] : maxval,
j > 0 ? 1 + ans[i][j - 1] : maxval
));
}
}
}
for(int i = m - 1; i >= 0; i--){
for(int j = n - 1; j >= 0; j--){
if(mat[i][j] != 0){
ans[i][j] = Math.min(ans[i][j], Math.min(
i < m - 1 ? 1 + ans[i + 1][j] : maxval,
j < n - 1 ? 1 + ans[i][j + 1] : maxval
));
}
}
}
}
}