Given an m×n binary matrix, mat, find the distance from each cell to the nearest 0. The distance between two adjacent cells is 1. Cells to the left, right, above, and below the current cell will be considered adjacent.
Constraints:
1≤ mat.row , mat.col≤50
1≤ mat.row * mat.col ≤2500
mat[i][j] ∈{0,1}
There is at least one 0 in mat.
So far, you have probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
A naive approach for this problem is to use a loop to iterate through every element in the matrix, and for every nonzero (1 in this case), we will iterate the whole matrix again and calculate the distance from that ...