This practice workspace is built for desktop. Open it on a larger screen to start solving.
Flood Fill
You are given an m x n integer grid image where image[i][j] represents the color of a pixel, and three integers sr, sc, and color. You should perform a flood fill starting at the pixel (sr, sc).
To perform a flood fill: starting from the source pixel, change the color of all 4-directionally connected pixels of the same original color to color. The replacement is recursive — once a pixel changes, its neighbors of the original color also change.
Return the modified image. The function must mutate the input grid and return it (test harness compares via deep equality).
Watch out. If color is the same as the starting pixel's original color, the image is unchanged — be careful not to recurse infinitely.
Examples
Constraints
- m == image.length
- n == image[i].length
- 1 <= m, n <= 50
- 0 <= image[i][j], color < 2^16
- 0 <= sr < m, 0 <= sc < n
Preview Mode
This is a read-only preview of DSAMind's AI coaching. Sign up to get custom feedback on your own solution.
Your approach
- Pattern:Depth-First Search
- Time:O(N)
- Space:O(1)
Complete approach — pattern and complexity both named.
Coach
What you got right
You correctly matched the sorted input requirement and implemented a linear-time scan using two pointers.
Where it diverged
No divergence detected. The code correctly aligns with the stated approach.
Next attempt: focus on
Try solving related sliding window problems to build familiarity with two-pointer variants.
Signals
No signals fired — clean run, you stayed in flow.