Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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
.
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
.0
in mat
.public class Solution {
public int[][] updateMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
queue.offer(new int[] {i, j});
}
else {
matrix[i][j] = Integer.MAX_VALUE;
}
}
}
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.isEmpty()) {
int[] cell = queue.poll();
for (int[] d : dirs) {
int r = cell[0] + d[0];
int c = cell[1] + d[1];
if (r < 0 || r >= m || c < 0 || c >= n ||
matrix[r][c] <= matrix[cell[0]][cell[1]] + 1) continue;
queue.add(new int[] {r, c});
matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
}
}
return matrix;
}
}
def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:
# BFS helper #
def bfs(node):
from collections import deque
q = deque()
i, j = node
q.append(((i,j), 0)) # d (dist to a zero) = 0 initially
visited = set()
dirs = [(1,0), (-1,0), (0,1), (0,-1)]
while q:
for i in range(len(q)):
coor, d = q.popleft()
x, y = coor
# if a zero nei is found
if matrix[x][y] == 0:
return d
visited.add(coor)
# investiagte neighbours
for dir in dirs:
newX, newY = x+dir[0], y+dir[1]
# within bounds:
if newX >= 0 and newX <= len(matrix)-1 and \
newY >= 0 and newY <= len(matrix[0])-1:
# not seen:
if (newX, newY) not in visited:
q.append(((newX, newY), d+1))
return -1
# main logic #
'''
steps:
- itertate over matrix to find cells = 1
- pass cells equaling 1 to a bfs to find the closest 0 to them
- update matrix
'''
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 1:
d = bfs((i,j)) # d = closest dist to a 0
matrix[i][j] = d # update M with d
return matrix
class Solution {
public:
bool isvalid(int i,int j,int m,int n)
{
if(i==m||j==n||j<0||i<0)
return false;
return true;
}
vector<vector<int>> dir={{1,0},{0,1},{0,-1},{-1,0}};
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix)
{
queue<pair<int,int>> q;
int m=matrix.size();
int n=matrix[0].size();
vector<vector<int>> dis(m,vector<int>(n,-1));
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
{
if(matrix[i][j]==0)
{
q.push({i,j});
dis[i][j]=0;
}
}
while(!q.empty())
{
pair<int,int> curr=q.front();
q.pop();
for(auto& x:dir)
{
int a=curr.first+x[0];
int b=curr.second+x[1];
if(isvalid(a,b,m,n)&&dis[a][b]==-1)
{
q.push({a,b});
dis[a][b]=dis[curr.first][curr.second]+1;
}
}
}
return dis;
}
};
In our experience, we suggest you solve this 01 Matrix LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.
If you are stuck anywhere between any coding problem, just visit Queslers to get the 01 Matrix LeetCode Solution
I hope this 01 Matrix LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.
This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.
Keep Learning!
More Coding Solutions >>