Grokking Graph Algorithms for Coding Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
01 Matrix (medium)
On this page

Problem Statement

Given an m x n matrix mat containing only 0 and 1, return the distance of the nearest 0 for each cell.

The distance between two adjacent cells of mat is 1.

Examples

Example 1:

  • Input: mat =
[[1,0,1,1],
 [1,1,1,1],
 [1,1,1,0]]
  • Expected Output:
[[1,0,1,2],
 [2,1,2,1],
 [3,2,1,0]]
  • Justification: Each cell updates to the minimum distance to a zero, with the edges being closest and moving inward, the distance increases.

Example 2:

  • Input: mat =
[[0,1,1],
 [1,1,0],
 [1,0,1]]

.....

.....

.....

Like the course? Get enrolled and start learning!

On this page