forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlood Fill.py
33 lines (22 loc) · 993 Bytes
/
Flood Fill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
queue = deque()
rows = len(image)
cols = len(image[0])
targetColor = image[sr][sc]
if color == targetColor:
# in this case, we don't need to do anything
return image
rDirs = [1, 0, -1, 0]
cDirs = [0, 1, 0, -1]
queue.append((sr, sc))
while len(queue) > 0:
r, c = queue.pop()
image[r][c] = color
for rd, cd in zip(rDirs, cDirs):
newRow = r + rd
newCol = c + cd
isValidCoordinate = newRow >= 0 and newRow < rows and newCol >= 0 and newCol < cols
if isValidCoordinate and image[newRow][newCol] == targetColor:
queue.append((newRow, newCol))
return image