Loading
Loading
A pixel-art editor stores an image as a grid of colour codes. Its paint bucket tool recolours the region under the cursor: the clicked pixel plus every pixel of the same colour that can be reached from it by stepping up, down, left or right through pixels of that colour.
Before repainting, the editor wants to show how many pixels will change. Given the grid canvas and the clicked position (row, col), return the number of pixels in that region. Diagonal neighbours do not count.
canvas = [[1,1,0,2],[0,1,1,2],[2,0,1,0],[2,2,1,1]], row = 0, col = 0(0,0)colour 1 — start the region(0,1)right of the start, also colour 1(1,1) (1,2)down from (0,1), then right(2,2) (3,2)straight down the middle column(3,3)right of (3,2); seven pixels in all7Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
| 0 | 1 | 1 | 0 | 2 |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 2 |
| 2 | 2 | 0 | 1 | 0 |
| 3 | 2 | 2 | 1 | 1 |
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(rows × cols) time · O(rows × cols) space
Input. Run your code to watch it work on this input, one line at a time.
| 0 | 1 | 1 | 0 | 2 |
|---|---|---|---|---|
| 1 | 0 | 1 | 1 | 2 |
| 2 | 2 | 0 | 1 | 0 |
| 3 | 2 | 2 | 1 | 1 |
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
Example 2
| 0 | 3 | 3 | 3 |
|---|---|---|---|
| 1 | 3 | 4 | 3 |
| 2 | 3 | 3 | 3 |
Example 3
| 0 | 5 | 5 |
|---|---|---|
| 1 | 5 | 5 |