LeetCode 733. Flood Fill (Algorithm Explained)

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


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

You have the ultimate power to make us understand complex problems very easily that's what makes you powerful. Your the best...

biswamohandwari
Автор

1:21 number of islands, flood fill, rotten basically bfs n dfs algos

vyshnavramesh
Автор

your mistake was a good thing to learn from

tushargupta
Автор

this line saved my day thanks! I was going with stackOverFlow exception without it.
if ( image[sr][sc] == newColor ) return image;

ahmadalk
Автор

Idk if you read these comments anymore, but I want to say thanks dude. These videos that you've made have been vital to my ability to understand how to approach LeetCode questions.

TheBsoncarty
Автор

The most blessed thing is that I am stuck with a question, but Nick has already solved it and made a video.

ruibinzhang
Автор

Thanks a lot for the clear explanation. Just keep doing this awesome work. It really means a lot to me.

kishoureb
Автор

So there are 732 other episodes of this series?

smellthel
Автор

You were right, this is almost identical to number of islands. Once I got this I figured that one out pretty quickly.

adityap
Автор

Keep doing that awesome work man! Got it instantly, your explanations are very clear.

thommy
Автор

Thanks so much for sharing this, but doesn't this cause repetition? for eg: void fill method is called for sr=1, sc=1, more than it is required.

prathyushakadali
Автор

What is the time and space complexity? I'm guessing time = O(n) and space = O(1). Is that right?

jacksingermusic
Автор

it's kinda funny how Nick is always High :D

theonlyme
Автор

hello Sir
i am getting stuck at the below test case
[[0, 0, 0], [1, 0, 0]]
1
0
2

sanskargupta
Автор

just very curious why you need to write another function, is it possible that return it in the same function? I mean use the recursive for the function floodfill

zn
Автор

The first line is essential; otherwise, the recursion function call will go into an infinite loop.

ruibinzhang
Автор

Your videos are really helpful man ! Thanks a ton

harinijeyaraman
Автор

Can anyone explain why sc >= image[0].length and not sc >= image.length ?

sanatgawade
Автор

Using same given function and not creating a separate method (I didn't feel the need)

public int[][] floodFill(int[][] image, int sr, int sc, int color) {
// find neighbours and add to stack
int val = image[sr][sc];
int left = sc-1 >= 0 ? image[sr][sc-1] : -1;
int right = sc+1 <= image[0].length-1? image[sr][sc+1] : -1;
int up = sr-1 >= 0 ? image[sr-1][sc] : -1;
int down = sr+1 <= image.length-1? image[sr+1][sc] : -1;

image[sr][sc] = color;
if (left == val && left != color) {
floodFill(image, sr, sc-1, color);
}
if (right == val && right != color) {
floodFill(image, sr, sc+1, color);
}
if (up == val && up != color) {
floodFill(image, sr-1, sc, color);
}
if (down == val && down != color) {
floodFill(image, sr+1, sc, color);
}

return image;
}

gillupenn
Автор

we optimize this by not visiting nodes again which are already visited right?

richa
join shbcf.ru