-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasygraphs_733_floodFill.py
43 lines (32 loc) · 1.24 KB
/
easygraphs_733_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
"""
https://leetcode.com/problems/combination-sum/
leetcode 733
easy
graphs
Logic : DFS
input : m x n integer grid image, three integers sr, sc, and color
output: list of list of int : the modified image after performing the flood fill.
explanation : perform DFS, check boundary conditions when searched in all 4 directions
Time Complexity: O(m * n), Space Complexity: O(m * n) for recursion stack, where m is the
number of row and n is the number of column.
"""
def floodFill(image, sr, sc, color):
def search_and_fill(row: int, column: int) -> None:
if image[row][column] == previos_color:
image[row][column] = color
if row + 1 < number_of_rows:
search_and_fill(row+1, column)
if row >= 1:
search_and_fill(row-1, column)
if column + 1 < number_of_columns:
search_and_fill(row, column+1)
if column >= 1:
search_and_fill(row, column-1)
number_of_columns, number_of_rows = len(image[0]), len(image)
previos_color = image[sr][sc]
if previos_color == color:
return image
search_and_fill(sr, sc)
return image
print(floodFill([[1,1,1],[1,1,0],[1,0,1]],1,1,2))
print(floodFill([[0,0,0],[0,0,0]],0,0,0))