Leetcode 733 Solution
This article provides solution to leetcode question 733 (flood-fill)
Access this page by simply typing in "lcs 733" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/flood-fill
Solution
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
m = len(image)
n = len(image[0])
q = [(sr, sc)]
seen = {(sr, sc)}
oldColor = image[sr][sc]
image[sr][sc] = newColor
while q:
r, c = q.pop(0)
if r > 0 and image[r - 1][c] == oldColor and (r - 1, c) not in seen:
q.append((r - 1, c))
seen.add((r - 1, c))
image[r - 1][c] = newColor
if r < m - 1 and image[r + 1][c] == oldColor and (r + 1, c) not in seen:
q.append((r + 1, c))
seen.add((r + 1, c))
image[r + 1][c] = newColor
if c > 0 and image[r][c - 1] == oldColor and (r, c - 1) not in seen:
q.append((r, c - 1))
seen.add((r, c - 1))
image[r][c - 1] = newColor
if c < n - 1 and image[r][c + 1] == oldColor and (r, c + 1) not in seen:
q.append((r, c + 1))
seen.add((r, c + 1))
image[r][c + 1] = newColor
return image