From 2df05e4971c75cde09b2f5fb9961cd2d4bf6858b Mon Sep 17 00:00:00 2001 From: PrakarshKamal Date: Tue, 10 Mar 2026 20:47:31 -0400 Subject: [PATCH] Complete DFS-1 --- 01Matrix.java | 48 +++++++++++++++++++++++++++++++++++ FloodFill.java | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 01Matrix.java create mode 100644 FloodFill.java diff --git a/01Matrix.java b/01Matrix.java new file mode 100644 index 00000000..dd23cd97 --- /dev/null +++ b/01Matrix.java @@ -0,0 +1,48 @@ +import java.util.*; + +// BFS O(n * m) time, O(n * m) space +class Solution { + public int[][] updateMatrix(int[][] mat) { + int n = mat.length; + int m = mat[0].length; + + int[][] dirs = new int[][] { + {0,1},{1,0},{-1,0},{0,-1} + }; + + Queue q = new LinkedList<>(); + + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { + if (mat[i][j] == 0) { + q.offer(new int[]{i, j}); + } + else { + mat[i][j] = -1; + } + } + } + + int distance = 1; + + while (!q.isEmpty()) { + int size = q.size(); + + for (int i = 0; i < size; i++) { + int[] curr = q.poll(); + for (int[] dir : dirs) { + int r = curr[0] + dir[0]; + int c = curr[1] + dir[1]; + + if (r >= 0 && c >= 0 && r < n && c < m && mat[r][c] == -1) { + q.offer(new int[] {r,c}); + mat[r][c] = distance; + } + } + + } + distance++; + } + return mat; + } +} diff --git a/FloodFill.java b/FloodFill.java new file mode 100644 index 00000000..0c23aaab --- /dev/null +++ b/FloodFill.java @@ -0,0 +1,68 @@ +// BFS O(n * m) time, O(n * m) space +// class Solution { +// public int[][] floodFill(int[][] image, int sr, int sc, int color) { +// int n = image.length; +// int m = image[0].length; + +// int[][] dirs = new int[][] { +// {0,1},{1,0},{-1,0},{0,-1} +// }; + +// int oldColor = image[sr][sc]; + +// if (oldColor == color) return image; + +// Queue q = new LinkedList<>(); +// q.offer(new int[] {sr, sc}); +// image[sr][sc] = color; + +// while (!q.isEmpty()) { +// int[] curr = q.poll(); + +// for (int[] dir : dirs) { +// int r = curr[0] + dir[0]; +// int c = curr[1] + dir[1]; + +// if (r >= 0 && c >= 0 && r < n && c < m && image[r][c] == oldColor) { +// q.offer(new int[] {r, c}); +// image[r][c] = color; +// } +// } +// } +// return image; +// } +// } + +// DFS O(n * m) time, O(n * m) space +class Solution { + public int[][] floodFill(int[][] image, int sr, int sc, int color) { + int n = image.length; + int m = image[0].length; + + int[][] dirs = new int[][] { + {0,1},{1,0},{-1,0},{0,-1} + }; + + int oldColor = image[sr][sc]; + + if (oldColor == color) return image; + + dfs(image, sr, sc, n, m, dirs, oldColor, color); + + return image; + } + + private void dfs(int[][] image, int i, int j, int n, int m, int[][] dirs, int oldColor, int color) { + + image[i][j] = color; + + for (int[] dir : dirs) { + int r = i + dir[0]; + int c = j + dir[1]; + + if (r >= 0 && c >= 0 && r < n && c < m && image[r][c] == oldColor) { + dfs(image, r, c, n, m, dirs, oldColor, color); + } + } + } +} \ No newline at end of file