Leetcode 733 – FloodFill

class Solution:
    
    def fill(self, image: List[List[int]], sr: int, sc: int, starting_color: int, newColor: int) -> List[List[int]]:
        if sr < 0 or sc < 0 or sr >= len(image) or sc >= len(image[0]) or image[sr][sc] != starting_color:
            return
        image[sr][sc] = newColor
        self.fill(image, sr + 1, sc, starting_color, newColor)
        self.fill(image, sr - 1, sc, starting_color, newColor)
        self.fill(image, sr, sc + 1, starting_color, newColor)
        self.fill(image, sr, sc - 1, starting_color, newColor)
        
    
    def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
        if image[sr][sc] == newColor:
            return image
        self.fill(image, sr, sc, image[sr][sc], newColor)
        return image

This is my solution for the flood fill problem in python.

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

Leave a Reply

Your email address will not be published. Required fields are marked *