Algorithm · Snowflake · Medium
Given a binary matrix mat with m rows and n columns, produce a matrix whose entry at each position is the distance to the closest cell containing 0. Moving between horizontally or vertically neighboring cells costs 1. Diagonal movement is not allowed. Examples Example 1 Input: mat = [[1,0,1],[1,1,1]] Output: [[1,0,1],[2,1,2]] Each value records the fewest cardinal moves needed to reach a zero; for instance, the bottom-left cell is two moves from the zero in the top-middle…
Checking your access…