Skip to content

Commit

Permalink
feature: flood fill revisit again
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Jan 10, 2024
1 parent bfe9c70 commit 1f2d834
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions arrays/FloodFill.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@

# O(n) time || O(n) space
def flood_fill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
color_to_change = image[sr][sc]
if color_to_change == color:
rows, cols = len(image), len(image[0])
original_color = image[sr][sc]
if original_color == color:
return image

def dfs(r, c):
if image[r][c] != color_to_change:
return

image[r][c] = color

if r > 0:
if 0 <= r < rows and 0 <= c < cols and image[r][c] == original_color:
image[r][c] = color
dfs(r - 1, c)

if r < len(image) - 1:
dfs(r + 1, c)

if c > 0:
dfs(r, c - 1)

if c < len(image[0]) - 1:
dfs(r, c + 1)

dfs(sr, sc)
Expand Down

0 comments on commit 1f2d834

Please sign in to comment.