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

Team TASEWA’s Expense Tracker Application

Making IT Teams More Effective at Project Management

Waifusion Community Update: Edition 4

Don’t like the new tab page of Chrome? Make your own!

Why are customers from the Nordic countries one of the best in the world?

Observer Design Pattern

Burp’s Recursive Grep for Owning Emdee five for life HTB Challenge

Unity Dev: Editor Layout for a more productive one

Unity Editor Default Layout

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

Data Structure: Arrays and Lists (II)

leetcode 290. Word Pattern

Kruskal’s Algorithm using Greedy method

Delete only one occurrence of 5 from a number and return the maximum number