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.
This is a multi-source BFS problem. Instead of starting from 1s and searching for 0s, we start from all 0s simultaneously and propagate distances outward. This ensures we find the shortest distance for each cell.
mat = [[0,0,0],[0,1,0],[0,0,0]][[0,0,0],[0,1,0],[0,0,0]]mat = [[0,0,0],[0,1,0],[1,1,1]][[0,0,0],[0,1,0],[1,2,1]]m == mat.lengthn == mat[i].length1 <= m, n <= 10^41 <= m * n <= 10^4mat[i][j] is either 0 or 1There is at least one 0 in matClick "Run" to execute your code against test cases
Socratic guidance - I'll ask questions, not give answers