Skip to content
Open

solved #1400

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
67 changes: 67 additions & 0 deletions 01_matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
Approach 01: Since we have connected components or neighbours to explore
here we can use BFS with queue approach.

For Brute Force we can do BFS for each and every cell or bucket

Time: m sqr n sqr(not optimal) --> time limit exceeded
Space: mxn

To Optimize this:
Instead of starting from 1's we can start from 0's
so we need not to repeat BFS and we can compute all ans together.
*/

// Time: O(mxn)
// Space: O(mxn)

// Code works fine over leetcode


class Solution {
public int[][] updateMatrix(int[][] mat) {

int m = mat.length;
int n = mat[0].length;

Queue<int[]> q = new LinkedList<>(); // queue for BFS
boolean[][] visited = new boolean[m][n]; // track processed cells

// Push all 0 cells into queue
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
q.offer(new int[]{i, j}); // source cell
visited[i][j] = true; // mark visited immediately
}
}
}

int[][] dirs = {{1,0}, {-1,0}, {0,1}, {0,-1}};

// BFS traversal
while (!q.isEmpty()) {

int[] curr = q.poll();
int r = curr[0];
int c = curr[1];

// Explore all 4 neighbours
for (int[] d : dirs) {

int nr = r + d[0];
int nc = c + d[1];

// Check boundary and ensure not already visited
if (nr >= 0 && nc >= 0 && nr < m && nc < n && !visited[nr][nc]) {

mat[nr][nc] = mat[r][c] + 1; // distance
visited[nr][nc] = true;
q.offer(new int[]{nr, nc}); // push neighbor into queue
}
}
}

return mat;
}
}
42 changes: 42 additions & 0 deletions Flood_Fill_Image.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
Approach: as we want to recolor all the connected pixels that have same color
As we already have four directions, it looks like graph traversal problem
Hence we can use depth-first-search, since we have all connected components here
*/

// Time : O(mxn) --> in worst case scenario every pixel in the image visited only once
// Space : O(mxn) --> as recursion stack in the worst case (entire grid filled with the same color).

// Yes the code successfully ran over leetcode.

class Solution {
int[][] dirs;
int m, n;
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
this.dirs = new int[][]{{-1,0}, {1,0}, {0,1}, {0,-1}};
this.m = image.length;
this.n = image[0].length;

int oldColor = image[sr][sc]; // store original color of start pixel
if(oldColor == color) return image; // check if the original color is equal to already new color

dfs(image, sr, sc, color, oldColor); // perform dfs and recolor the current pixel

return image;

}

private void dfs(int[][] image, int i, int j, int color, int oldColor) {

if(i < 0 || j < 0 || i == m || j == n || image[i][j] != oldColor) return;

image[i][j] = color; // recolor the current pixel

for(int[] dir : dirs) {
int r = dir[0] + i;
int c = dir[1] + j;

{dfs(image, r, c, color, oldColor);} // recursively call dfs to check all connected components
}
}
}
7 changes: 0 additions & 7 deletions Sample.java

This file was deleted.