#542 01 Matrix

Description

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.

Examples

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 <= 1 0 4 10^4 104
1 <= m * n <= 1 0 4 10^4 104
mat[i][j] is either 0 or 1.
There is at least one 0 in mat.

思路

最开始的时候肯定想的是暴力求解,分三步走

  • 找到所有0所在的位置
  • 计算当前节点和所有0位置的相对关系
  • 找到最小距离更新返回的矩阵

很明显这种暴力方法是会面临TTL的制裁的
所以需要乖乖用DP,不过刚开始做DP的时候,还困在暴力的泥沼之中,所以dp了但没完全dp,后面发现反正dp需要两次求解(只循环一次的话会存在更新不到位的问题)
举个例子
[0 1 1 1 1]
[1 1 0 1 0]
[1 0 1 1 1]
如果只更新一次的话(从上到下从左到右),会得到
[0 1 2 3 4]
[1 2 0 1 0]
[2 0 1 2 1]
很明显这个是不对的,但再经历一次dp(从下到上从右到左),会得到
[0 1 1 2 1]
[1 1 0 1 0]
[1 0 1 2 1]
继续上文,反正dp需要两次求解,所以第一次求解的时候直接用 max = mat.length * mat[0].length 来初始化就可以了,不需要具体求解真实位置。

代码

暴力

class Point {
    public int x;
    public int y;
    
    Point(int _x, int _y){
        this.x = _x;
        this.y = _y;
    }
}
class Solution {
    public int calDistance(Point a, Point b){
        return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
    }
    
    public List<Point> findZeros(int[][] mat) {
        List<Point> points = new ArrayList<>();
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                if (mat[i][j] == 0) {
                    points.add(new Point(i, j));
                }
            }
        }
        
        return points;
    }
    
    public int[][] updateMatrix(int[][] mat) {
        List<Point> zeros = findZeros(mat);
        int[][] answer = new int[mat.length][mat[0].length];
        
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                answer[i][j] = Integer.MAX_VALUE;
                for (Point p: zeros){
                    answer[i][j] = Math.min(answer[i][j], Math.abs(p.x - i) + Math.abs(p.y - j));
                }
            }
        }
        
        return answer;
    }
}

DP但不完全DP

class Point {
    public int x;
    public int y;
    
    Point(int _x, int _y){
        this.x = _x;
        this.y = _y;
    }
}
class Solution {
    public int calDistance(Point a, Point b){
        return Math.abs(a.x - b.x) + Math.abs(a.y - b.y);
    }
    
    public List<Point> findZeros(int[][] mat) {
        List<Point> points = new ArrayList<>();
        for (int i = 0; i < mat.length; i++) {
            for (int j = 0; j < mat[0].length; j++) {
                if (mat[i][j] == 0) {
                    points.add(new Point(i, j));
                }
            }
        }
        
        return points;
    }
    
    public int[][] updateMatrix(int[][] mat) {
        List<Point> zeros = findZeros(mat);
        int[][] answer = new int[mat.length][mat[0].length];
        
        for (int i = 0; i < mat.length; i++){
            answer[i][0] = Integer.MAX_VALUE;
            if (mat[i][0] == 0)
                answer[i][0] = 0;
            else
                for (Point p: zeros) 
                    answer[i][0] = Math.min(answer[i][0], Math.abs(p.x - i) + p.y);
            
            answer[i][mat[0].length - 1] = Integer.MAX_VALUE;
            if (mat[i][mat[0].length - 1] == 0)
                answer[i][mat[0].length - 1] = 0;
            else
                for (Point p: zeros) 
                    answer[i][mat[0].length - 1] = Math.min(answer[i][mat[0].length - 1], Math.abs(p.x - i) + (mat[0].length - 1 - p.y));
        }
        
        for (int j = 0; j < mat[0].length; j++) {
            answer[0][j] = Integer.MAX_VALUE;
            if (mat[0][j] == 0)
                answer[0][j] = 0;
            else
                for (Point p: zeros)
                    answer[0][j] = Math.min(answer[0][j], Math.abs(p.y - j) + p.x);
            
            answer[mat.length - 1][j] = Integer.MAX_VALUE;
            if (mat[mat.length - 1][j] == 0)
                answer[mat.length - 1][j] = 0;
            else
                for (Point p: zeros)
                    answer[mat.length - 1][j] = Math.min(answer[mat.length - 1][j], Math.abs(p.y - j) + (mat.length - 1 - p.x));
        }
        
        for (int i = 1; i < mat.length - 1; i++) {
            for (int j = 1; j < mat[0].length - 1; j++) {
                if (mat[i][j] == 0)
                    answer[i][j] = 0;
                else
                    answer[i][j] = Math.min(answer[i][j - 1], answer[i - 1][j]) + 1;
            }
        }
        
        for (int i = mat.length - 2; i > 0; i--) {
            for (int j = mat[0].length - 2; j > 0; j--) {
                answer[i][j] = Math.min(answer[i][j], answer[i + 1][j] + 1);
                answer[i][j] = Math.min(answer[i][j], answer[i][j + 1] + 1);
            }
        }
        
        return answer;
    }
}

DP(这一份是直接拷贝的submission,没有自己写了)

class Solution {
    public int[][] updateMatrix(int[][] mat) {
      if (mat.length == 0 || mat[0].length == 0) {
        return mat;
    }
    int[][] dis = new int[mat.length][mat[0].length];
    int range = mat.length * mat[0].length;
    
    for (int i = 0; i < mat.length; i++) {
        for (int j = 0; j < mat[0].length; j++) {
            if (mat[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 = mat.length - 1; i >= 0; i--) {
        for (int j = mat[0].length - 1; j >= 0; j--) {
            if (mat[i][j] == 0) {
                dis[i][j] = 0;
            } else {
                int downCell = (i < mat.length - 1) ? dis[i + 1][j] : range;
                int rightCell = (j < mat[0].length - 1) ? dis[i][j + 1] : range;
                dis[i][j] = Math.min(Math.min(downCell, rightCell) + 1, dis[i][j]);
            }
        }
    }
    
    return dis;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值