Flood Fill | LeetCode 733 | C++, Java, Python | May LeetCoding Day 11

preview_player
Показать описание

**** Best Books For Data Structures & Algorithms for Interviews:**********
*****************************************************************************

MayLeetCoding Challenge | Problem 11 | Flood Fill | 11 May,
Facebook Coding Interview question,
google coding interview question,
leetcode,
Flood Fill,
Flood Fill c++,
Flood Fill Java,
Flood Fill python,
Flood Fill solution,

#Facebook #CodingInterview #LeetCode #MayLeetCodingChallenge #Google #FloodFill #Amazon
Рекомендации по теме
Комментарии
Автор

Very clean code and nice explanation...👍👍👍

anushkabagchi
Автор

Hi, I didn’t get why stack overflow raised when it missed to set new colour. Of course the result will be wrong but why the stack overflow?

shameem.pullaratt
Автор

Thanks for the help this was kind of new concept...

williamwambua
Автор

My java code I have used your DFS methods explained as in ISLANDS question:
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int color) {

int r=image.length;
int c=image[0].length;
int i, j;
int x=image[sr][sc];
if( image[sr][sc]==color)
return image;
else
dfs(image, sr, sc, color, x);
return image;

}
void dfs(int[][] image, int x, int y, int color, int num)
{
if(image[x][y]!=color) {
image[x][y]=color;
if(x+1<image.length && image[x+1][y]==num)

dfs(image, x+1, y, color, num);

if(x-1>=0 && image[x-1][y]==num)

dfs(image, x-1, y, color, num);
if(y+1<image[0].length && image[x][y+1]==num)


dfs(image, x, y+1, color, num);



if(y-1>=0 && image[x][y-1]==num)


dfs(image, x, y-1, color, num);
}




}
}

ruchithaparmeshwar
Автор

Can you please explain why is the oldColor parameter used??

devalmodi
Автор

why do we need the first if condition, i.e if(image[sr][sc] == newColor) return image?

HarshSharma-vdrx
Автор

Just a small doubt.why c>=image[0].length and not c>=image.length as same as the row.

arudhranarasimhan
Автор

Please check the code im getting recursive depth limit exceeded



class Solution:
def floodFill(self, image: List[List[int]], sr: int, sc: int, newColor: int) -> List[List[int]]:

color = image[sr][sc]
if color == newColor: return image

self.flood_fill(image, sr, sc, newColor, color)
return image

def flood_fill(self, image: List[List[int]], r: int, c: int, newColor: int, oldColor:int):

if (r<0 or c<0 or r>len(image) or c>len(image[0]) or oldColor!= image[r][c]):
return None

image[r][c] == newColor;
self.flood_fill(image, r+1, c, newColor, oldColor);
self.flood_fill(image, r-1, c, newColor, oldColor);
self.flood_fill(image, r, c+1, newColor, oldColor);
self.flood_fill(image, r, c-1, newColor, oldColor);

TheSahilpadyal
visit shbcf.ru