Created
January 27, 2023 00:00
-
-
Save SohanChy/e781f388dc22f2107735f67ba4cd6efd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]: | |
lim_y = len(image) - 1 | |
if lim_y == 0: | |
return None | |
lim_x = len(image[0]) - 1 | |
y = sr | |
x = sc | |
set_done = set() | |
li_todo = [] | |
li_todo.append((y,x)) | |
while len(li_todo) > 0: | |
current = li_todo.pop(0) | |
last_color = image[current[0]][current[1]] | |
image[current[0]][current[1]] = color | |
set_done.add(current) | |
left = (current[0],current[1]-1) | |
right = (current[0],current[1]+1) | |
if left[1] >= 0 and last_color == image[left[0]][left[1]] and left not in set_done: | |
li_todo.append(left) | |
if right[1] <= lim_x and last_color == image[right[0]][right[1]] and right not in set_done: | |
li_todo.append(right) | |
up = (current[0]-1,current[1]) | |
down = (current[0]+1,current[1]) | |
if up[0] >= 0 and last_color == image[up[0]][up[1]] and up not in set_done: | |
li_todo.append(up) | |
if down[0] <= lim_y and last_color == image[down[0]][down[1]] and down not in set_done: | |
li_todo.append(down) | |
return image |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment