-
Notifications
You must be signed in to change notification settings - Fork 92
/
ZeroOneMatrix542.java
101 lines (93 loc) · 2.96 KB
/
ZeroOneMatrix542.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* Given a matrix consists of 0 and 1, find the distance of the nearest 0 for
* each cell.
*
* The distance between two adjacent cells is 1.
*
* Example 1:
* Input:
* 0 0 0
* 0 1 0
* 0 0 0
* Output:
* 0 0 0
* 0 1 0
* 0 0 0
*
* Example 2:
* Input:
* 0 0 0
* 0 1 0
* 1 1 1
* Output:
* 0 0 0
* 0 1 0
* 1 2 1
* Note:
* The number of elements of the given matrix will not exceed 10,000.
* There are at least one 0 in the given matrix.
* The cells are adjacent in only four directions: up, down, left and right.
*/
public class ZeroOneMatrix542 {
private int[][] directions = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public int[][] updateMatrix(int[][] matrix) {
int M = matrix.length;
int N = matrix[0].length;
Queue<int[]> q = new LinkedList<>();
for (int i=0; i<M; i++) {
for (int j=0; j<N; j++) {
if (matrix[i][j] == 1) {
matrix[i][j] = Integer.MAX_VALUE;
} else {
q.add(new int[]{i, j});
}
}
}
while (!q.isEmpty()) {
int[] curr = q.poll();
int i = curr[0];
int j = curr[1];
for (int[] dir: directions) {
int x = i + dir[0];
int y = j + dir[1];
if (x < 0 || y < 0 || x >= M || y >= N || matrix[x][y] <= matrix[i][j] || matrix[x][y] == 0) continue;
q.add(new int[]{x, y});
matrix[x][y] = matrix[i][j] + 1;
}
}
return matrix;
}
/**
* https://leetcode.com/problems/01-matrix/discuss/101051/Simple-Java-solution-beat-99-(use-DP)
*/
public int[][] updateMatrix2(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return matrix;
}
int[][] dis = new int[matrix.length][matrix[0].length];
int range = matrix.length * matrix[0].length;
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
dis[i][j] = 0;
} else {
int upCell = (i > 0) ? dis[i - 1][j] : range;
int leftCell = (j > 0) ? dis[i][j - 1] : range;
dis[i][j] = Math.min(upCell, leftCell) + 1;
}
}
}
for (int i = matrix.length - 1; i >= 0; i--) {
for (int j = matrix[0].length - 1; j >= 0; j--) {
if (matrix[i][j] == 0) {
dis[i][j] = 0;
} else {
int downCell = (i < matrix.length - 1) ? dis[i + 1][j] : range;
int rightCell = (j < matrix[0].length - 1) ? dis[i][j + 1] : range;
dis[i][j] = Math.min(Math.min(downCell, rightCell) + 1, dis[i][j]);
}
}
}
return dis;
}
}