01 Matrix
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. **Examples:** ```typescript updateMatrix([[0,0,0],[0,1,0],[0,0,0]]) // [[0,0,0],[0,1,0],[0,0,0]] updateMatrix([[0,0,0],[0,1,0],[1,1,1]]) // [[0,0,0],[0,1,0],[1,2,1]] ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(m * n)
- Space complexity: O(m * n)
Multi-source BFS visits each cell at most once in O(m * n) time.
Follow-Up Questions Interviewers Ask About 01 Matrix
- Why start BFS from all 0s rather than from each 1?
Starting from all 0s is multi-source BFS running in O(m*n) total time. Starting from each 1 would be O((m*n)^2).
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.