1988coder

542. 01 Matrix

Apr 16th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.13 KB | None | 0 0
  1. // LeetCode URL: https://leetcode.com/problems/01-matrix/
  2.  
  3. /**
  4.  * Dynamic Programming
  5.  *
  6.  * Refer: 1)
  7.  * https://leetcode.com/problems/01-matrix/discuss/101051/Simple-Java-solution-beat-99-(use-DP)
  8.  * 2)
  9.  * https://leetcode.com/problems/01-matrix/discuss/101051/Simple-Java-solution-beat-99-(use-DP)/113437
  10.  *
  11.  * The first iteration is from upper left corner to lower right. It's trying to
  12.  * update dis[i][j] to be the distance to the nearest 0 that is in the top left
  13.  * region relative to (i,j). If there's a nearer 0 to the right or to the bottom
  14.  * of (i,j), it won't catch that. And because of the direction of the double
  15.  * loop, it's already in correct iterative order, meaning, you must have dealt
  16.  * with dis[i-1][j] and dis[i][j-1] before you deal with dis[i][j]
  17.  *
  18.  * Then in the second loop, it goes the opposite direction from the lower right
  19.  * corner. So it'll find the distance to the nearest 0 in the bottom right
  20.  * region. Now combine that with the result from the first loop, it'll cover
  21.  * nearest 0 in all directions. That is where dis[i][j] takes the min of its
  22.  * previous value from the first loop, and the new value (distance to the
  23.  * nearest 0 in the lower right region)
  24.  *
  25.  * Time Complexity: O(2 * M * N)
  26.  *
  27.  * Space Complexity: O(M * N) - Including the result space. O(1) - Excluding the
  28.  * result space.
  29.  *
  30.  * M = Number of rows. N = Number of columns.
  31.  */
  32. class Solution {
  33.     public int[][] updateMatrix(int[][] matrix) {
  34.         if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
  35.             return new int[0][0];
  36.         }
  37.  
  38.         int rows = matrix.length;
  39.         int cols = matrix[0].length;
  40.         int[][] result = new int[rows][cols];
  41.  
  42.         for (int i = 0; i < rows; i++) {
  43.             for (int j = 0; j < cols; j++) {
  44.                 if (matrix[i][j] != 0) {
  45.                     // If the cell in original matrix is not 0, then set this result cell to
  46.                     // Integer.MAX_VALUE. This will help to find the minimum in the following
  47.                     // conditions.
  48.                     result[i][j] = Integer.MAX_VALUE;
  49.                     if (i > 0 && result[i - 1][j] != Integer.MAX_VALUE) {
  50.                         result[i][j] = result[i - 1][j] + 1;
  51.                     }
  52.                     if (j > 0 && result[i][j - 1] != Integer.MAX_VALUE) {
  53.                         result[i][j] = Math.min(result[i][j], result[i][j - 1] + 1);
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.         for (int i = rows - 1; i >= 0; i--) {
  60.             for (int j = cols - 1; j >= 0; j--) {
  61.                 // Integer.MAX_VALUE check can be removed from below conditions if there is at
  62.                 // least one 0 in the given matrix
  63.                 if (i < rows - 1 && result[i + 1][j] != Integer.MAX_VALUE) {
  64.                     result[i][j] = Math.min(result[i][j], result[i + 1][j] + 1);
  65.                 }
  66.                 if (j < cols - 1 && result[i][j + 1] != Integer.MAX_VALUE) {
  67.                     result[i][j] = Math.min(result[i][j], result[i][j + 1] + 1);
  68.                 }
  69.             }
  70.         }
  71.  
  72.         return result;
  73.     }
  74. }
Add Comment
Please, Sign In to add comment