[
dfs
bfs
2d-array
]
BinarySearch 0128 Paint Bucket
Problem statement
https://binarysearch.com/problems/Paint-Bucket/
Solution
Equal to Leetcode 0733 Flood Fill.
Complexity
Time and space complexity O(mn)
.
Code
class Solution:
def solve(self, image, r, c, target):
def dfs(x, y):
if not 0 <= x < w or not 0 <= y < h or image[x][y] == target: return
if image[x][y] == oldColor:
image[x][y] = target
for dx, dy in neibs_list:
dfs(x + dx, y + dy)
w, h, oldColor = len(image), len(image[0]), image[r][c]
neibs_list = [[-1, 0], [1, 0], [0, -1], [0, 1]]
dfs(r, c)
return image