Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions 01Matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// TimeComplexity: O(m*n)
// SpaceComplexity: O(m*n)
// Explanation: I start by marking all 1s as -1 to indicate unvisited cells and enqueue all 0-cells.
// Then, I perform BFS layer by layer, updating each neighbor cell with its distance from the nearest 0.
// I increase the distance after each BFS layer to ensure all cells get the minimum distance from a 0.
// This guarantees every cell is updated once and efficiently.
class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
Queue<Integer> q = new LinkedList<>();
int[][] dirs = {{1,0},{-1,0},{0,1},{0,-1}};
for(int i =0; i<m ; i++) {
for(int j=0; j<n; j++) {
if(mat[i][j] == 1){
mat[i][j] = -1;
}
if(mat[i][j] == 0) {
q.add(i);
q.add(j);
}
}
}
int dist =1;
while(!q.isEmpty()) {
int size = q.size();
for(int i=0; i<size; i=i+2) {
int r= q.poll();
int c = q.poll();
for(int[] dir : dirs ){
int nr = r+dir[0];
int nc = c+dir[1];
if(nr>=0 && nr<m && nc>=0 && nc<n){
if(mat[nr][nc] == -1){
mat[nr][nc] = dist;
q.add(nr);
q.add(nc);
}
}
}
}
dist++;
}
return mat;
}
}
67 changes: 67 additions & 0 deletions FloodFill.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@

// Solution -1

// TimeComplexity: O(m*n);
// SpaceComplexity: O(m*n)
// Explanation: I traverse the image using DFS starting from the given cell. I color each cell and recursively visit its neighbors that have the original color.
// This ensures all connected cells are updated exactly once.
class Solution {
int[][] dirs = {{0,-1}, {0,1}, {1,0}, {-1,0}};
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
if(image[sr][sc] == color) return image;
dfs(sr,sc,image,color, image[sr][sc]);
return image;
}

private void dfs(int sr, int sc, int[][] image, int color,int initColor) {
image[sr][sc] = color;
for(int i =0; i<dirs.length; i ++) {
int r = sr+ dirs[i][0];
int c = sc+ dirs[i][1];
if(r>=0 && r<image.length && c>=0 && c<image[0].length) {
if(image[r][c] == initColor) {
image[r][c] = color;
dfs(r,c,image,color, initColor);
}
}
}

}
}



// Solution-2

// TimeComplexity: O(m*n);
// SpaceComplexity: O(m*n)
// Explanation: I traverse the image using BFS starting from the given cell. I color each cell and enqueue its neighbors with the original color.
// This ensures all connected cells are updated efficiently, layer by layer.
// class Solution {
// public int[][] floodFill(int[][] image, int sr, int sc, int color) {
// if(image[sr][sc] == color) return image;
// int[][] dirs = {{0,-1}, {0,1}, {1,0}, {-1,0}};
// int initColor = image[sr][sc];
// image[sr][sc] = color;
// Queue<Integer> q = new LinkedList<>();
// q.add(sr);
// q.add(sc);
// while(!q.isEmpty()) {
// int currR = q.poll();
// int currC = q.poll();
// for(int i =0; i<dirs.length; i ++) {
// int r = currR+ dirs[i][0];
// int c = currC+ dirs[i][1];
// if(r>=0 && r<image.length && c>=0 && c<image[0].length) {
// if(image[r][c] == initColor) {
// image[r][c] = color;
// q.add(r);
// q.add(c);
// }
// }
// }
// }
// return image;

// }
// }