-
Notifications
You must be signed in to change notification settings - Fork 1
/
floodFill.py
65 lines (49 loc) · 1.82 KB
/
floodFill.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Recursion problem
Approach : Depth-first search
'''
from typing import List
# Very slow
class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, color: int) -> List[List[int]]:
if image[sr][sc] == color:
return image
def subFill(image, sr, sc, color, start):
if sr < 0 or sc < 0 or sr >= len(image) or sc >= len(image[0]):
return image
if image[sr][sc] == start:
image[sr][sc] = color
subFill(image, sr - 1, sc, color, start)
subFill(image, sr + 1, sc, color, start)
subFill(image, sr, sc - 1, color, start)
subFill(image, sr, sc + 1, color, start)
start = image[sr][sc]
subFill(image, sr, sc, color, start)
return image
# Slight better
class Solution2:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:
m, n = len(image), len(image[0])
color = image[sr][sc]
if color == newColor: return image
def subFill(r, c):
if image[r][c] == color:
image[r][c] = newColor
if r >= 1: subFill(r - 1, c)
if r < m - 1: subFill(r + 1, c)
if c >= 1: subFill(r, c - 1)
if c < n - 1: subFill(r, c + 1)
subFill(sr, sc)
return image
# Fastest
class Solution3:
def floodFill(self, image, sr, sc, newColor):
def dfs(i, j):
image[i][j] = newColor
for x, y in ((i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)):
if 0 <= x < m and 0 <= y < n and image[x][y] == old:
dfs(x, y)
old, m, n = image[sr][sc], len(image), len(image[0])
if old != newColor:
dfs(sr, sc)
return image