From cc04bd485171a5dd74f406a66c9a604fdd067390 Mon Sep 17 00:00:00 2001 From: Dheepthi-Reddy Date: Thu, 25 Dec 2025 23:19:45 -0600 Subject: [PATCH] Solved DFS-1 --- FloodFill.py | 38 +++++++++++++++++++++++++++ LeetCode_01_matrix.py | 61 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 FloodFill.py create mode 100644 LeetCode_01_matrix.py diff --git a/FloodFill.py b/FloodFill.py new file mode 100644 index 00000000..67875374 --- /dev/null +++ b/FloodFill.py @@ -0,0 +1,38 @@ +''' +In this problem we need to update the color of given pixel with gieven color, and also the adjacent pixels if they have same color +Here I am using DFS approach to solve this problem, we start at the given index and change its color and check its neighbors if any of them is same color as the original color we change its color +After changing the color we check the neighbors of current pixel if they have same color we change its color. +If none of the neighbors have the same color, we go back to parent pixel where we started. Once all the pixel values are updated recursion ends. +''' +class Solution: + def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: + + m = len(image) + n = len(image[0]) + dirs = [[-1,0], [1,0], [0, 1], [0, -1]] + # saving the original color + originalColor = image[sr][sc] + if originalColor == color: + return image + + self.dfs(image, sr, sc, color, originalColor, dirs) + return image + + def dfs(self, image, r, c, color, originalColor, dirs): + # base: out of bound or pixel color is not same as the original clor + if (r < 0 or c < 0 or r == len(image) or c == len(image[0]) or image[r][c] != originalColor): return + + # logic + image[r][c] = color + # finding the neighboring coordinates and calling dfs recursively + for dir in dirs: + newRow = r + dir[0] + newCol = c + dir[1] + self.dfs(image, newRow, newCol, color, originalColor, dirs) + +''' +Time Complexity: O(m*n) +In worst case we iterate on each of the element in the grid once. +Space Complexity: O(m*n) +In worst case we need m*n space, space taken by recursive tree is equal to the height of the tree. +''' \ No newline at end of file diff --git a/LeetCode_01_matrix.py b/LeetCode_01_matrix.py new file mode 100644 index 00000000..519742cb --- /dev/null +++ b/LeetCode_01_matrix.py @@ -0,0 +1,61 @@ +''' +In this problem we need to find the distance of nearest 0 to 1. +Here I am running DFS for every cell which has value as 1 to find nearest 0, and updating the resultant matrix with distance values. +Using these neighboring result values, we compute the next neighboring values distance. +''' +class Solution: + def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]: + if mat is None or len(mat) == 0: return mat + self.m = len(mat) + self.n = len(mat[0]) + dirs = [[-1, 0], [1, 0], [0, 1], [0, -1]] + self.result = [[0]* self.n for _ in range(self.m)] + # looping through each cell + for i in range(self.m): + for j in range(self.n): + if mat[i][j] == 1: + self.result[i][j] = self.dfs(mat, i, j) + + return self.result + + def dfs(self, mat, i, j): + # base: if any immediate neighbor is 0, update the distance to 1 and stop recursion + if i > 0 and mat[i-1][j] == 0: + return 1 + if i < self.m-1 and mat[i+1][j] == 0: + return 1 + if j > 0 and mat[i][j-1] == 0: + return 1 + if j < self.n-1 and mat[i][j+1] == 0: + return 1 + + # taking the distances of 4 directions(represnting infinity) + top, left, bottom, right = 9999, 9999, 9999, 9999 + # here we are going top-down, so if the top values are already calculated we can reuse them + # top + if i > 0 and self.result[i-1][j] != 0: + top = self.result[i-1][j] + # left + if j > 0 and self.result[i][j-1] != 0: + left = self.result[i][j-1] + + # for right and bottom we are computing using DFS + # right + if j < self.n-1: + if self.result[i][j+1] == 0: + self.result[i][j+1] = self.dfs(mat, i, j+1) + right = self.result[i][j+1] + # bottom + if i < self.m-1: + if self.result[i+1][j] == 0: + self.result[i+1][j] = self.dfs(mat, i+1, j) + bottom = self.result[i+1][j] + + return 1+ min(top, min(left, min(bottom, right))) + +''' +Time Complexity: O(m*n) +In worst case if all the cells are 1, time taken to go through each cell is m*n +Space Complexity: O(m*n) +In worst case if all the cells are 1, maximum stack space used by the tree is m*n +''' \ No newline at end of file