← DiffPush

Flood Fill

Baseline Graphs · Traversal Problems O(m * n) · O(m * n)

Repainting the Contiguous Rack Zones

A paint robot on a warehouse mezzanine receives one coordinate and a new shade. It repaints the crate under the nozzle, then rolls to the four edge-adjacent crates — but only across crates that currently wear the exact same shade as the starting one — continuing the spread until the whole matching zone wears the new colour. Crates of a different shade, even a step away, stay untouched.

Input: An m x n integer grid image, a start cell (sr, sc), and an integer color.

Output: Return the grid after repainting every cell 4-directionally connected to the start that shared the start's original colour.

Constraints

Examples

Example 1

Input: {"image":[[1,1,1],[1,1,0],[1,0,1]],"sr":1,"sc":1,"color":2}
Output: [[2,2,2],[2,2,0],[2,0,1]]
The connected 1-zone around (1,1) turns 2; the far 1 is diagonally cut off and keeps its colour.

Example 2

Input: {"image":[[0,0],[0,1]],"sr":0,"sc":0,"color":0}
Output: [[0,0],[0,1]]
Repainting a zone with its own shade is a no-op everywhere.

Solve this in your browser →

Also on LeetCode ↗