Skip to content

Commit

Permalink
feature: flood fill revisit
Browse files Browse the repository at this point in the history
  • Loading branch information
solairerove committed Jan 10, 2024
1 parent a63735c commit bfe9c70
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions arrays/FloodFill.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@
# 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 == color_to_change:
if color_to_change == color:
return image

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

if r >= 1:
dfs(r - 1, c)
image[r][c] = color

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

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

if c < len(image[0]) - 1:
dfs(r, c + 1)
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 bfe9c70

Please sign in to comment.