#542

01 Matrix

specialist · 815 · lc medium +31 · verified · 53.4% accepted · 10,685 likes · top 44%

play →

Description

Given an m x n binary matrix mat, produce a matrix of the same size where each cell's value is the shortest distance (in steps between edge-adjacent cells) from that cell to the nearest 0 in mat.

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]]

Code

1
2
3