Medium

Quiz

#542 01 Matrix

APPROACH

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]]
1 of 4
1:00

What is the optimal approach for this problem?