Skip to the code
cf.
+10 XP

#420 Paint Bucket Tool

easy

You're building the paint bucket for a pixel-art editor. The canvas image is a grid of color numbers. Clicking pixel (row, col) with color color repaints that pixel and every pixel connected to it (up, down, left, or right, through pixels of the same original color) with color.

Write paintBucket(image, row, col, color) that applies the fill and returns the canvas. Diagonal pixels are not connected.

Example 1

Input: image = [[1,1,1],[1,1,0],[1,0,1]], row = 1, col = 1, color = 2
Output: [[2,2,2],[2,2,0],[2,0,1]]

Every 1 connected to the center becomes 2. The bottom-right 1 only touches the others diagonally, so it stays.

Example 2

Input: image = [[0,0],[0,0]], row = 0, col = 0, color = 0
Output: [[0,0],[0,0]]

The pixel already has color 0, so nothing changes.

Constraints

  • 1 <= image.length, image[i].length <= 50
  • 0 <= image[i][j], color <= 65535
  • (row, col) is inside the canvas.
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).