Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc].
To perform a flood fill:
color.Return the modified image after performing the flood fill.
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]]
Explanation:
From the center of the image with position (sr, sc) = (1, 1) (i.e., the red pixel), all pixels connected by a path of the same color as the starting pixel (i.e., the blue pixels) are colored with the new color.
Note the bottom corner is not colored 2, because it is not horizontally or vertically connected to the starting pixel.
Example 2:
Input: image = [[0,0,0],[0,0,0]], sr = 0, sc = 0, color = 0
Output: [[0,0,0],[0,0,0]]
Explanation:
The starting pixel is already colored with 0, which is the same as the target color. Therefore, no changes are made to the image.
Constraints:
m == image.lengthn == image[i].length1 <= m, n <= 500 <= image[i][j], color < 2160 <= sr < m0 <= sc < nProblem summary: You are given an image represented by an m x n grid of integers image, where image[i][j] represents the pixel value of the image. You are also given three integers sr, sc, and color. Your task is to perform a flood fill on the image starting from the pixel image[sr][sc]. To perform a flood fill: Begin with the starting pixel and change its color to color. Perform the same process for each pixel that is directly adjacent (pixels that share a side with the original pixel, either horizontally or vertically) and shares the same color as the starting pixel. Keep repeating this process by checking neighboring pixels of the updated pixels and modifying their color if it matches the original color of the starting pixel. The process stops when there are no more adjacent pixels of the original color to update. Return the modified image after performing the flood fill.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array
[[1,1,1],[1,1,0],[1,0,1]] 1 1 2
[[0,0,0],[0,0,0]] 0 0 0
island-perimeter)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #733: Flood Fill
class Solution {
private int[][] image;
private int oc;
private int color;
private final int[] dirs = {-1, 0, 1, 0, -1};
public int[][] floodFill(int[][] image, int sr, int sc, int color) {
oc = image[sr][sc];
if (oc == color) {
return image;
}
this.image = image;
this.color = color;
dfs(sr, sc);
return image;
}
private void dfs(int i, int j) {
image[i][j] = color;
for (int k = 0; k < 4; ++k) {
int x = i + dirs[k], y = j + dirs[k + 1];
if (x >= 0 && x < image.length && y >= 0 && y < image[0].length && image[x][y] == oc) {
dfs(x, y);
}
}
}
}
// Accepted solution for LeetCode #733: Flood Fill
func floodFill(image [][]int, sr int, sc int, color int) [][]int {
m, n := len(image), len(image[0])
oc := image[sr][sc]
if oc == color {
return image
}
dirs := []int{-1, 0, 1, 0, -1}
var dfs func(i, j int)
dfs = func(i, j int) {
image[i][j] = color
for k := 0; k < 4; k++ {
x, y := i+dirs[k], j+dirs[k+1]
if x >= 0 && x < m && y >= 0 && y < n && image[x][y] == oc {
dfs(x, y)
}
}
}
dfs(sr, sc)
return image
}
# Accepted solution for LeetCode #733: Flood Fill
class Solution:
def floodFill(
self, image: List[List[int]], sr: int, sc: int, color: int
) -> List[List[int]]:
def dfs(i: int, j: int):
image[i][j] = color
for a, b in pairwise(dirs):
x, y = i + a, j + b
if 0 <= x < len(image) and 0 <= y < len(image[0]) and image[x][y] == oc:
dfs(x, y)
oc = image[sr][sc]
if oc != color:
dirs = (-1, 0, 1, 0, -1)
dfs(sr, sc)
return image
// Accepted solution for LeetCode #733: Flood Fill
impl Solution {
pub fn flood_fill(mut image: Vec<Vec<i32>>, sr: i32, sc: i32, color: i32) -> Vec<Vec<i32>> {
let m = image.len();
let n = image[0].len();
let sr = sr as usize;
let sc = sc as usize;
let oc = image[sr][sc];
if oc == color {
return image;
}
let dirs = [-1, 0, 1, 0, -1];
fn dfs(
image: &mut Vec<Vec<i32>>,
i: usize,
j: usize,
oc: i32,
color: i32,
m: usize,
n: usize,
dirs: &[i32; 5],
) {
image[i][j] = color;
for k in 0..4 {
let x = i as isize + dirs[k] as isize;
let y = j as isize + dirs[k + 1] as isize;
if x >= 0 && x < m as isize && y >= 0 && y < n as isize {
let x = x as usize;
let y = y as usize;
if image[x][y] == oc {
dfs(image, x, y, oc, color, m, n, dirs);
}
}
}
}
dfs(&mut image, sr, sc, oc, color, m, n, &dirs);
image
}
}
// Accepted solution for LeetCode #733: Flood Fill
function floodFill(image: number[][], sr: number, sc: number, color: number): number[][] {
const [m, n] = [image.length, image[0].length];
const oc = image[sr][sc];
if (oc === color) {
return image;
}
const dirs = [-1, 0, 1, 0, -1];
const dfs = (i: number, j: number): void => {
image[i][j] = color;
for (let k = 0; k < 4; k++) {
const [x, y] = [i + dirs[k], j + dirs[k + 1]];
if (x >= 0 && x < m && y >= 0 && y < n && image[x][y] === oc) {
dfs(x, y);
}
}
};
dfs(sr, sc);
return image;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.