Ctrl K

Paint Bucket Tool

Easy
A picture is an m × n matrix canvas, where canvas[i][j] is a pixel's color. The user clicks the paint bucket with color paint on the pixel at row row and column col. The bucket recolors that pixel to paint, together with every pixel of the same original color reachable from it by stepping between side-adjacent neighbors (up, down, left, right) through pixels of that color only. Diagonal neighbors do not count. Return the picture after the fill.
Example 1
canvas=[[3, 3, 0], [0, 3, 3], [3, 0, 3]], row=0, col=1, paint=7 → [[7, 7, 0], [0, 7, 7], [3, 0, 7]]

From cell (0, 1) the 3s at (0,0), (0,1), (1,1), (1,2), (2,2) are joined by sides and become 7. The 3 at (2,0) touches them only by a corner and stays.

Example 2
canvas=[[5, 5], [5, 5]], row=1, col=0, paint=5 → [[5, 5], [5, 5]]

The paint matches the area's color, so nothing changes.

Example 3
canvas=[[1, 2, 1]], row=0, col=2, paint=0 → [[1, 2, 0]]
Constraints
  • 1 ≤ m, n ≤ 50
  • 0 ≤ canvas[i][j], paint < 2¹⁶
  • 0 ≤ row < m
  • 0 ≤ col < n
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsIsland Coastline Length EasyDrain the Landlocked Ponds MediumLand Regions on a Map MediumFrame Around an Ink Blot HardLongest Uphill Walk Hard
loading Python…
Input
canvas=[[3, 3, 0], [0, 3, 3], [3, 0, 3]], row=0, col=1, paint=7
Expected
[[7, 7, 0], [0, 7, 7], [3, 0, 7]]

Run checks the first 3 tests in your browser. Submit checks all 91 tests on the server, hidden ones included.