...

/

01 Matrix

01 Matrix

Try to solve the 01 Matrix problem.

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] {0,1}\in \{0, 1\}

  • There is at least one 00 in mat.

Example

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

01 Matrix

1.

Given a matrix, mat = [[0, 0, 0], [0, 1, 0], [1, 0, 0]], find the matrix that contains the distance of the nearest 0 for each cell.

A.

[[0, 0, 0], [0, 0, 0], [0, 0, 0]]

B.

[[0, 0, 0], [0, 1, 0], [1, 0, 0]]

C.

[[0, 1, 0], [0, 1, 0], [0, 1, 0]]

D.

[[1, 0, 1], [0, 1, 0], [1, 0, 1]]


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6

Try it yourself

Implement your solution in the following coding playground.

C++
usercode > UpdateMatrix.cpp
std::vector<std::vector<int>> UpdateMatrix(std::vector<std::vector<int>> mat) {
// Replace this placeholder return statement with your code
return {};
}
01 Matrix

Access this course and 1200+ top-rated courses and projects.