美文网首页机试
542. 01 Matrix

542. 01 Matrix

作者: Nancyberry | 来源:发表于2018-04-27 08:57 被阅读0次

Description

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:

  1. The number of elements of the given matrix will not exceed 10,000.
  2. There are at least one 0 in the given matrix.
  3. The cells are adjacent in only four directions: up, down, left and right.

Solution

BFS, time O(m * n), space O(m * n)

跟"286. Walls and Gates"完全相同的解法。
注意,这道题用的是BFS level traversal,而非Dijikstra,所以需要在进队列的时候更新dis,而非出队列的时候!

class Solution {
    public static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
    
    public int[][] updateMatrix(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return matrix;
        }
        
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dis = new int[m][n];
        for (int[] row : dis) {
            Arrays.fill(row, Integer.MAX_VALUE);
        }
        
        Queue<int[]> queue = new LinkedList<>();
        
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (matrix[i][j] == 0) {
                    queue.offer(new int[]{i, j});
                    dis[i][j] = 0;
                }
            }
        }
        
        while (!queue.isEmpty()) {
            int[] pos = queue.poll();
            int neighborDis = dis[pos[0]][pos[1]] + 1;
            
            for (int[] direction : DIRECTIONS) {
                int x = pos[0] + direction[0];
                int y = pos[1] + direction[1];
                // check isValid here! otherwise dis[x][y] might be broken
                if (x < 0 || x >= m || y < 0 || y >= n || dis[x][y] < Integer.MAX_VALUE) {
                    continue;
                }

                dis[x][y] = neighborDis;
                queue.offer(new int[] {x, y});
            }
        }
        
        return dis;
    }
}

DFS: TLE

DP, time O(mn), space O(mn)

也可以用DP来解,因为每个位置(i, j)的结果只依赖与它上下左右邻居到0的距离。

用两遍遍历即可解决啦。有一个坑是,由于后面会对邻居的dis加一进行比较,所以不能将dis[][]初始化成Integer.MAX_VALUE,会overflow的!

class Solution {
    public int[][] updateMatrix(int[][] matrix) {
        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return matrix;
        }
        
        int m = matrix.length;
        int n = matrix[0].length;
        int[][] dis = new int[m][n];
        for (int[] row : dis) {
            Arrays.fill(row, 10001);    // shouldn't use Integer.MAX_VALUE! Add cause overflow
        }
        
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (matrix[i][j] == 0) {
                    dis[i][j] = 0;
                    continue;
                }
                
                if (i > 0) {
                    dis[i][j] = Math.min(1 + dis[i - 1][j], dis[i][j]);
                }
                
                if (j > 0) {
                    dis[i][j] = Math.min(1 + dis[i][j - 1], dis[i][j]);
                }
            }
        }
        
        for (int i = m - 1; i >= 0; --i) {
            for (int j = n - 1; j >= 0; --j) {
                if (i < m - 1) {
                    dis[i][j] = Math.min(1 + dis[i + 1][j], dis[i][j]);
                }
                if (j < n - 1) {
                    dis[i][j] = Math.min(1 + dis[i][j + 1], dis[i][j]);
                }
            }
        }
        
        return dis;
    }
}

相关文章

  • 542. 01 Matrix

    Given a matrix consists of 0 and 1, find the distance of ...

  • 542. 01 Matrix

    这道题不看答案,没想出思路,做题还是不够,看了bfs的思路,然后手写了一遍,记录一下。这道题从bfs的角度来看,思...

  • [Java] 542. 01 Matrix

    Description Given a matrix consists of 0 and 1, find the ...

  • 2018-09-18 542. 01 Matrix

    题意:给你一个矩阵只包含元素0和1,求的一个矩阵,该矩阵在原矩阵为1的位置得出该元素距离最近的0的距离(仅能上下左...

  • LeetCode 542. 01 矩阵

    542. 01 矩阵 题目来源:https://leetcode-cn.com/problems/01-matri...

  • Lecture 03

    01. Matrix Multiplication (4 ways) 02. Inverse Matrix 03....

  • 542. 01 矩阵

    题目描述 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。 ...

  • 542. 01矩阵

    题目描述 给定一个由0和1组成的矩阵,找出每个元素到最近0的距离,相邻元素间的距离为1。例如:输入 输出 注意: ...

  • ARTS打卡第一周

    ARTS打卡第一周 Algorithm:每周至少做一个 leetcode 的算法题 542. 01 矩阵 Revi...

  • 01 Matrix

    题目来源给一个01矩阵,求矩阵中的1最近的0离它有多远,更新矩阵变为一个距离矩阵。我想着从左上到右下遍历一遍,从右...

网友评论

    本文标题:542. 01 Matrix

    本文链接:https://www.haomeiwen.com/subject/xlmwlftx.html