542. 01 Matrix [Medium]

/**
 * 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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值