01 Matrix
MediumP2Coding
Given a binary matrix mat, return a matrix of the same size where each cell holds the distance to the nearest 0. Distance is the number of up, down, left or right steps between two cells. The matrix contains at least one 0.
Example 1
- Input
- mat = [[0,0,0],[0,1,0],[0,0,0]]
- Output
- [[0,0,0],[0,1,0],[0,0,0]]
Example 2
- Input
- mat = [[0,0,0],[0,1,0],[1,1,1]]
- Output
- [[0,0,0],[0,1,0],[1,2,1]]
Constraints
- 1 <= rows, cols <= 10^4 with rows * cols <= 10^4
- mat[i][j] is 0 or 1
- At least one 0 exists.
Hints
Give it a real attempt first. Run shows which examples fail.
Saved automatically
Run checks the 3 visible tests, Submit all 9. ⌘Enter runs, ⌘⇧Enter submits.