← All problems
Flood Fill
An image is represented by an m x n integer grid where each value is a pixel color. Given a starting pixel (sr, sc) and a new color, perform a flood fill: change the starting pixel and all 4-directionally connected pixels of the same original color to the new color, then return the modified image. If the new color equals the original color, return the image unchanged.
Complexity
Time O(m*n), space O(m*n) worst case (recursion stack / queue).
Common pitfalls
- Forgetting the same-color early return causes infinite recursion.
- Using 8-directional connectivity when the problem specifies 4-directional.
- Comparing against the new color instead of the captured original color during spread.
Your solution
Log in to save your code and track progress.