0542 01 Matrix
Solved at: 230130
Question
Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
The distance between two adjacent cells is 1.
Solution
Results
- Time taken: 11 m 43 s
- Runtime 1856 ms, Beats 10.39%
- Memory 15.5 MB, Beats 83.12%
Complexity Analysis
- Time:
- Space:
Where is the cost -- the number of iterations in which "BFS clear" operation will run.
Other Answers Online
Dynamic Programming in 2 passes. An easier solution will be 4 passes from left → right, right → left, top → bottom, bottom → top.
Backlinks (1)
Comments (0)