01 Matrix
Find the distance of the nearest 0 for each cell in a binary matrix using multi-source BFS.
01The problem
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.
| Input | Output | Why |
|---|---|---|
mat = [[0,0,0],[0,1,0],[1,1,1]] | [[0,0,0],[0,1,0],[1,2,1]] | Distance from each 1 to its nearest 0. |
Constraints
m == mat.lengthn == mat[i].length1 <= m, n <= 10^41 <= m * n <= 10^4mat[i][j]is either0or1.- There is at least one
0inmat.
Loading the rest of the lesson…