Solution to Leetcode’s Flood Fill

flood fill description

CONCEPT

splitting of array elements
flood fill transition steps
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
if (image[sr][sc] == newColor) return image;

flood(image, image[sr][sc], newColor, sr, sc);

return image;
}

public void flood(int[][] image, int color, int newColor, int sr, int sc) {
if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length || image[sr][sc] != color) {
return;
}

image[sr][sc] = newColor;

int topRowNeighbour = sr - 1;
int bottomRowNeighbour = sr + 1;
int leftColumnNeigbour = sc - 1;
int rightColumnNeighbour = sc + 1;

flood(image, color, newColor, topRowNeighbour, sc );
flood(image, color, newColor, bottomRowNeighbour, sc );
flood(image, color, newColor, sr, leftColumnNeigbour );
flood(image, color, newColor, sr, rightColumnNeighbour );
}
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why I migrated from WordPress to Medium

Single Responsibility and the Heart of Darkness

Top 10 Python Developer Skills You Must Know

About a Matrix Creator, a Raspberry PI and Azure.

Windows 11 without TPM?

Enable Intel Platform Trust Technology BIOS Screenshot

Saas , Sass & SCSS Interview Related Topics

Building your first app in Flutter

What is Functional Testing? — A Complete Guide To Automation Tools

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Chunks

Chunks

More from Medium

Solve: Greater elements

LeetCode 217. Contains Duplicate

Leetcode Q514. Freedom Trail (Q433)

Leetcode 42 — Trapping Rain Water