Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

542. 01 Matrix

Medium49.9% Acceptance
ArrayDynamic ProgrammingBreadth-First Search
Asked by:
Google
ProblemSolutions (12)VideosCompanies (17)Notes

Problem Statement

Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.

The distance between two cells sharing a common edge is 1.

Example 1:

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

Example 2:

Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]

Constraints:

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 104
  • 1 <= m * n <= 104
  • mat[i][j] is either 0 or 1.
  • There is at least one 0 in mat.

Note: This question is the same as 1765: https://leetcode.com/problems/map-of-highest-peak/

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
G
A
Apple
B
Bloomberg
B
Bytedance
M
Microsoft
+12

Approach

The goal of #542 01 Matrix is to compute the distance of every cell containing 1 to its nearest 0 in a binary matrix. A common and efficient idea is to treat all 0 cells as starting points and expand outward to update the distance of neighboring cells.

The most popular approach uses multi-source Breadth-First Search (BFS). First, push all cells containing 0 into a queue. Then perform BFS to propagate the shortest distance to adjacent cells. Each step updates neighboring 1 cells with the minimum distance from the nearest zero.

Another approach uses Dynamic Programming with two passes over the matrix. The first pass checks top and left neighbors, while the second pass checks bottom and right neighbors to refine the distances.

Both strategies avoid repeatedly scanning the matrix for each cell. The BFS method is intuitive for shortest-path problems in grids, while the DP method is space-efficient and relies on local transitions.

Complexity

ApproachTime ComplexitySpace Complexity
Multi-source BFSO(m × n)O(m × n)
Dynamic Programming (Two Pass)O(m × n)O(1) extra or O(m × n) depending on implementation

Video Solution Available

take U forward

View all video solutions

Solutions (12)

Breadth-First Search (BFS) Approach

The BFS approach involves initializing a queue with all positions of zeros in the matrix, since the distance from any zero to itself is zero. From there, perform a level-order traversal (BFS) to update the distances of the cells that are accessible from these initial zero cells. This approach guarantees that each cell is reached by the shortest path.

Time Complexity: O(m * n), as each cell is processed at most once.
Space Complexity: O(m * n), for storing the resulting distance matrix and the BFS queue.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4
5#define QUEUE_SIZE 10000
6





Explanation

In the C implementation, we use a queue to perform a BFS starting from all cells with 0. We then update neighboring cells with the smallest distance possible, propagating the minimum distances to eventually fill out the entire matrix with correct values.

Dynamic Programming Approach

The dynamic programming (DP) approach updates the matrix by considering each cell from four possible directions, iterating twice over the matrix to propagate the minimum distances. First, traverse from top-left to bottom-right, and then from bottom-right to top-left, ensuring a comprehensive minimum distance calculation.

Time Complexity: O(m * n)
Space Complexity: O(m * n)

CC++JavaPythonC#JavaScript
1#include




Video Solutions

Watch expert explanations and walkthroughs

G-13. Distance of nearest cell having 1 | 0/1 Matrix | C++ | Java

take U forward
20:21295,156 views

Asked By Companies

17 companies
G
Google
A
Apple
B
Bloomberg
B
Bytedance
M
Microsoft
U
Uber
M
Mathworks
A
Amazon
P
Publicis Sapient
D
Dunzo
F
Flipkart
U
Upstox
M
MTX
F
Fareye Technologies
W
Wipro
F
Facebook
A
Adobe

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Trapping Rain WaterHard
Jump Game IIMedium
Maximum SubarrayMedium
Jump GameMedium
More similar problems

Related Topics

ArrayDynamic ProgrammingBreadth-First SearchMatrix

Problem Stats

Acceptance Rate49.9%
DifficultyMedium
Companies17

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Is 01 Matrix asked in FAANG interviews?

Yes, 01 Matrix is a common medium-level interview problem related to graph traversal and dynamic programming. It tests understanding of BFS in grids and optimization techniques for shortest distance problems.

Why is multi-source BFS used in 01 Matrix?

Multi-source BFS allows all zero cells to act as starting points simultaneously. This ensures that each cell gets the minimum distance from the nearest zero while visiting each cell only once.

What is the optimal approach for 01 Matrix?

The optimal approach is usually multi-source BFS. By starting from all cells containing 0 and expanding outward, we compute the shortest distance to each 1 efficiently. This avoids running BFS from every cell individually.

What data structure is best for solving 01 Matrix?

A queue is the key data structure when using the BFS approach. It helps process cells level by level while updating distances to neighboring cells. For the DP approach, the matrix itself stores intermediate distance values.

7
int
directions
[
4
]
[
2
]
=
{
{
1
,
0
}
,
{
-
1
,
0
}
,
{
0
,
1
}
,
{
0
,
-
1
}
}
;
8
9
typedef
struct
{
10
int
row
,
col
;
11
}
Point
;
12
13
int
*
*
updateMatrix
(
int
*
*
mat
,
int
matSize
,
int
*
matColSize
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
14
*
returnSize
=
matSize
;
15
*
returnColumnSizes
=
malloc
(
sizeof
(
int
)
*
matSize
)
;
16
int
n
=
matColSize
[
0
]
;
17
int
*
*
dist
=
(
int
*
*
)
malloc
(
sizeof
(
int
*
)
*
matSize
)
;
18
for
(
int
i
=
0
;
i
<
matSize
;
++
i
)
{
19
dist
[
i
]
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
n
)
;
20
(
*
returnColumnSizes
)
[
i
]
=
n
;
21
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
22
dist
[
i
]
[
j
]
=
mat
[
i
]
[
j
]
==
0
?
0
:
INT_MAX
;
23
}
24
}
25
26
Point queue
[
QUEUE_SIZE
]
;
27
int
front
=
0
,
back
=
0
;
28
for
(
int
i
=
0
;
i
<
matSize
;
++
i
)
{
29
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
30
if
(
mat
[
i
]
[
j
]
==
0
)
{
31
queue
[
back
++
]
=
(
Point
)
{
i
,
j
}
;
32
}
33
}
34
}
35
36
while
(
front
<
back
)
{
37
Point p
=
queue
[
front
++
]
;
38
for
(
int
d
=
0
;
d
<
4
;
++
d
)
{
39
int
r
=
p
.
row
+
directions
[
d
]
[
0
]
;
40
int
c
=
p
.
col
+
directions
[
d
]
[
1
]
;
41
if
(
r
>=
0
&&
r
<
matSize
&&
c
>=
0
&&
c
<
n
&&
dist
[
r
]
[
c
]
>
dist
[
p
.
row
]
[
p
.
col
]
+
1
)
{
42
dist
[
r
]
[
c
]
=
dist
[
p
.
row
]
[
p
.
col
]
+
1
;
43
queue
[
back
++
]
=
(
Point
)
{
r
,
c
}
;
44
}
45
}
46
}
47
48
return
dist
;
49
}
<stdio.h>
2
#
include
<stdlib.h>
3
4
int
min
(
int
a
,
int
b
)
{
5
return
a
<
b
?
a
:
b
;
6
}
7
8
int
*
*
updateMatrix
(
int
*
*
mat
,
int
matSize
,
int
*
matColSize
,
int
*
returnSize
,
int
*
*
returnColumnSizes
)
{
9
*
returnSize
=
matSize
;
10
*
returnColumnSizes
=
malloc
(
sizeof
(
int
)
*
matSize
)
;
11
int
n
=
matColSize
[
0
]
;
12
int
*
*
dist
=
(
int
*
*
)
malloc
(
sizeof
(
int
*
)
*
matSize
)
;
13
for
(
int
i
=
0
;
i
<
matSize
;
++
i
)
{
14
dist
[
i
]
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
n
)
;
15
(
*
returnColumnSizes
)
[
i
]
=
n
;
16
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
17
dist
[
i
]
[
j
]
=
mat
[
i
]
[
j
]
==
0
?
0
:
INT_MAX
-
1
;
18
}
19
}
20
21
for
(
int
i
=
0
;
i
<
matSize
;
++
i
)
{
22
for
(
int
j
=
0
;
j
<
n
;
++
j
)
{
23
if
(
i
>
0
)
24
dist
[
i
]
[
j
]
=
min
(
dist
[
i
]
[
j
]
,
dist
[
i
-
1
]
[
j
]
+
1
)
;
25
if
(
j
>
0
)
26
dist
[
i
]
[
j
]
=
min
(
dist
[
i
]
[
j
]
,
dist
[
i
]
[
j
-
1
]
+
1
)
;
27
}
28
}
29
30
for
(
int
i
=
matSize
-
1
;
i
>=
0
;
--
i
)
{
31
for
(
int
j
=
n
-
1
;
j
>=
0
;
--
j
)
{
32
if
(
i
<
matSize
-
1
)
33
dist
[
i
]
[
j
]
=
min
(
dist
[
i
]
[
j
]
,
dist
[
i
+
1
]
[
j
]
+
1
)
;
34
if
(
j
<
n
-
1
)
35
dist
[
i
]
[
j
]
=
min
(
dist
[
i
]
[
j
]
,
dist
[
i
]
[
j
+
1
]
+
1
)
;
36
}
37
}
38
39
return
dist
;
40
}

Explanation

In this C code, distance calculation progresses first from the top-left to bottom-right. We then perform a second pass from bottom-right to top-left to integrate the shortest distances possible from alternate directions. The use of INT_MAX - 1 is a sufficient surrogate for infinity.