Flood Fill LeetCode solution in Python
1class Solution:2def floodFill(3self, image: List[List[int]], sr: int, sc: int, color: int4) -> List[List[int]]:5queue = deque()6rows = len(image)7cols = len(image[0])89targetColor = image[sr][sc]1011if color == targetColor:12# in this case, we don't need to do anything13return image1415rDirs = [1, 0, -1, 0]16cDirs = [0, 1, 0, -1]1718queue.append((sr, sc))1920while len(queue) > 0:21r, c = queue.pop()2223image[r][c] = color24for rd, cd in zip(rDirs, cDirs):25newRow = r + rd26newCol = c + cd2728isValidCoordinate = (29newRow >= 0 and newRow < rows and newCol >= 0 and newCol < cols30)3132if isValidCoordinate and image[newRow][newCol] == targetColor:33queue.append((newRow, newCol))3435return image