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

0% completed

01 Matrix (medium)
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

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!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible