← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
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. Cells are adjacent in the four cardinal directions.
m == mat.lengthn == mat[i].length1 <= m, n <= 10^51 <= m * n <= 10^5mat[i][j] is either 0 or 1.0 in the matrix.Input:
mat = [ [0,2,0], [0,1,0], [0,0,1] ]
Output:
[ [0,0,0], [0,1,0], [0,0,1] ]
Explanation:
The nearest 0 for each cell is shown in the output matrix. For cells with 0, the distance is 0.
Input:
mat = [ [1,0,2,0], [0,0,0,1], [1,0,1,0] ]
Output:
[ [0,0,0,0], [0,0,0,0], [0,0,0,0] ]
Use Breadth-First Search (BFS) to find the shortest distance from each cell to the nearest 0.