




Sponsored
Sponsored
The BFS approach involves initializing a queue with all positions of zeros in the matrix, since the distance from any zero to itself is zero. From there, perform a level-order traversal (BFS) to update the distances of the cells that are accessible from these initial zero cells. This approach guarantees that each cell is reached by the shortest path.
Time Complexity: O(m * n), as each cell is processed at most once.
Space Complexity: O(m * n), for storing the resulting distance matrix and the BFS queue.
1function updateMatrix(mat) {
2    const m = mat.length;
3    const n = mat[0].length;
4    const dist = Array.from({ length: m }, () => Array(n).fill(Infinity));
5    const queue = [];
6
7    for (let i = 0; i < m; i++) {
8        for (let j = 0; j < n; j++) {
9            if (mat[i][j] === 0) {
10                dist[i][j] = 0;
11                queue.push([i, j]);
12            }
13        }
14    }
15
16    const directions = [[1, 0], [-1, 0], [0, 1], [0, -1]];
17
18    while (queue.length) {
19        const [row, col] = queue.shift();
20        for (const [dr, dc] of directions) {
21            const newRow = row + dr;
22            const newCol = col + dc;
23            if (newRow >= 0 && newRow < m && newCol >= 0 && newCol < n && dist[newRow][newCol] > dist[row][col] + 1) {
24                dist[newRow][newCol] = dist[row][col] + 1;
25                queue.push([newRow, newCol]);
26            }
27        }
28    }
29
30    return dist;
31}JavaScript uses a queue for BFS, setting each zero-sourced cell initially and iterating directions until shortest paths for distance have been found and recorded throughout the matrix.
The dynamic programming (DP) approach updates the matrix by considering each cell from four possible directions, iterating twice over the matrix to propagate the minimum distances. First, traverse from top-left to bottom-right, and then from bottom-right to top-left, ensuring a comprehensive minimum distance calculation.
Time Complexity: O(m * n)
Space Complexity: O(m * n)
1public class Solution {
    public int[][] UpdateMatrix(int[][] mat) {
        int m = mat.Length, n = mat[0].Length;
        int[][] dist = new int[m][];
        for (int i = 0; i < m; i++) {
            dist[i] = new int[n];
            for (int j = 0; j < n; j++) {
                dist[i][j] = mat[i][j] == 0 ? 0 : int.MaxValue - 1;
            }
        }
        for (int i = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j) {
                if (mat[i][j] != 0) {
                    if (i > 0) dist[i][j] = Math.Min(dist[i][j], dist[i - 1][j] + 1);
                    if (j > 0) dist[i][j] = Math.Min(dist[i][j], dist[i][j - 1] + 1);
                }
            }
        }
        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) dist[i][j] = Math.Min(dist[i][j], dist[i + 1][j] + 1);
                    if (j < n - 1) dist[i][j] = Math.Min(dist[i][j], dist[i][j + 1] + 1);
                }
            }
        }
        return dist;
    }
}C# executes this double pass to ensure dist array is filled optimally from reference of initial positions, ultimately accumulating the shortest paths through systematic comprehensive traversal of rows & columns.