Problem statement

https://leetcode.com/problems/flood-fill/

Solution

Just do classical dfs (or bfs).

Complexity

Time and space complexity O(mn).

Code

class Solution:
    def floodFill(self, image, sr, sc, newColor):
        def dfs(x, y):
            if not 0 <= x < w or  not 0 <= y < h or image[x][y] == newColor: return 

            if image[x][y] == oldColor:
                image[x][y] = newColor
                for dx, dy in neibs_list: 
                    dfs(x + dx, y + dy)

        w, h, oldColor = len(image), len(image[0]), image[sr][sc]
        neibs_list = [[-1, 0], [1, 0], [0, -1], [0, 1]]

        dfs(sr, sc)
        return image