From d307a3c8d8d44f18ca2fbd2232f0176da8fa81f1 Mon Sep 17 00:00:00 2001 From: jeetusingh247 Date: Thu, 19 Feb 2026 19:09:54 +0530 Subject: [PATCH] solved --- 01_matrix.java | 67 +++++++++++++++++++++++++++++++++++++++++++ Flood_Fill_Image.java | 42 +++++++++++++++++++++++++++ Sample.java | 7 ----- 3 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 01_matrix.java create mode 100644 Flood_Fill_Image.java delete mode 100644 Sample.java diff --git a/01_matrix.java b/01_matrix.java new file mode 100644 index 00000000..7d82a12e --- /dev/null +++ b/01_matrix.java @@ -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 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; + } +} \ No newline at end of file diff --git a/Flood_Fill_Image.java b/Flood_Fill_Image.java new file mode 100644 index 00000000..64c6dcf4 --- /dev/null +++ b/Flood_Fill_Image.java @@ -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 + } + } +} \ No newline at end of file diff --git a/Sample.java b/Sample.java deleted file mode 100644 index f5c45b5f..00000000 --- a/Sample.java +++ /dev/null @@ -1,7 +0,0 @@ -// Time Complexity : -// Space Complexity : -// Did this code successfully run on Leetcode : -// Any problem you faced while coding this : - - -// Your code here along with comments explaining your approach \ No newline at end of file