Search⌘ K
AI Features

Solution: 01 Matrix

Explore how to solve the 01 Matrix problem by applying dynamic programming principles. Understand optimal substructure and overlapping subproblems to compute the minimum distance to the nearest zero in a matrix. This lesson guides you through an optimized algorithm with O(m×n) time complexity and constant space use.

Statement

Given an m×nm \times n binary matrix, mat, find the distance from each cell to the nearest 00. The distance between two adjacent cells is 11. Cells to the left, right, above, and below the current cell will be considered adjacent.

Constraints:

  • 11 \leq mat.row , mat.col50\leq 50

  • 11 \leq mat.row * mat.col 2500\leq 2500

  • mat[i][j] ...