/**
* discuss看的,这题必须从左上到右下一次,再从右下到左上一次,否则无法让每个点获得全部的access
* 时间复杂度 O(mn)
* Runtime: 7 ms, faster than 82.69%
* Memory Usage: 42.3 MB, less than 59.65%
*/
class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] dp = new int[m][n];
// from left up to right down
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] != 0) {
dp[i][j] = Integer.MAX_VALUE;
if (i > 0 && dp[i - 1][j] != Integer.MAX_VALUE) {
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j] + 1);
}
if (j > 0 && dp[i][j - 1] != Integer.MAX_VALUE) {
dp[i][j] = Math.min(dp[i][j], dp[i][j - 1] + 1);
}
}
}
}
// from right down to left up
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (mat[i][j] != 0) {
if (i < m - 1 && dp[i + 1][j] != Integer.MAX_VALUE) {
dp[i][j] = Math.min(dp[i][j], dp[i + 1][j] + 1);
}
if (j < n - 1 && dp[i][j + 1] != Integer.MAX_VALUE) {
dp[i][j] = Math.min(dp[i][j], dp[i][j + 1] + 1);;
}
}
}
}
return dp;
}
}
/**
* 也可以直接用mat矩阵,不需要dp
* Runtime: 6 ms, faster than 91.98%
* Memory Usage: 42.5 MB, less than 49.13%
*/
class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
// from left up to right down
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] != 0) {
mat[i][j] = Integer.MAX_VALUE;
if (i > 0 && mat[i - 1][j] != Integer.MAX_VALUE) {
mat[i][j] = Math.min(mat[i][j], mat[i - 1][j] + 1);
}
if (j > 0 && mat[i][j - 1] != Integer.MAX_VALUE) {
mat[i][j] = Math.min(mat[i][j], mat[i][j - 1] + 1);
}
}
}
}
// from right down to left up
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (mat[i][j] != 0) {
if (i < m - 1 && mat[i + 1][j] != Integer.MAX_VALUE) {
mat[i][j] = Math.min(mat[i][j], mat[i + 1][j] + 1);
}
if (j < n - 1 && mat[i][j + 1] != Integer.MAX_VALUE) {
mat[i][j] = Math.min(mat[i][j], mat[i][j + 1] + 1);;
}
}
}
}
return mat;
}
}
/**
* BFS
* 时间复杂度O(mn):第一次双层循环O(mn),第二次while循环是走遍矩阵每一条边,也是O(mn)
* Runtime: 14 ms, faster than 48.14%
* Memory Usage: 40.9 MB, less than 90.83%
*/
class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
Queue<int[]> q = new LinkedList(); // store points to start from
// collect all the nodes with distance 0 as the start points
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
q.offer(new int[]{i, j});
} else {
mat[i][j] = Integer.MAX_VALUE;
}
}
}
// BFS
int[][] dirs = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0,1}};
while (!q.isEmpty()) {
int[] start = q.poll();
for (int[] dir : dirs) {
int x = start[0] + dir[0], y = start[1] + dir[1];
if (x < 0 || x >= m || y < 0 || y >= n || mat[x][y] <= mat[start[0]][start[1]] + 1) {
continue;
}
mat[x][y] = mat[start[0]][start[1]] + 1;
q.offer(new int[]{x, y}); // store as next points to start from
}
}
return mat;
}
}
/** * discuss看的,这题必须从左上到右下一次,再从右下到左上一次,否则无法让每个点获得全部的access * Runtime: 7 ms, faster than 82.69% * Memory Usage: 42.3 MB, less than 59.65% */class Solution { public int[][] updateMatrix(int[][] mat) { int m = mat.length, n = mat[0].length;.