Flood Fill | Leetcode

  Рет қаралды 54,365

Techdose

Techdose

Күн бұрын

This video explains a very important interview question based on depth first search,i.e., recursion and backtracking. The problem name is flood fill. This is a very good problem to learn and practice DFS and recursion. I have explained the problem with intuition for solving. I have shown the solution with proper example and have also explained the CODE at the end of the video. CODE LINK is present below as usual. If you find any difficulty or have any query then do COMMENT below. PLEASE help our channel by SUBSCRIBING and LIKE our video if you found it helpful...CYA :)
CODE LINK: gist.github.co...

Пікірлер: 112
@rohitsrivastava5414
@rohitsrivastava5414 4 жыл бұрын
You are most under rated tech content creator, it's so simply explained every single yime, keep up the great work man, hope to see more videos about DS Algo and maybe system design concepts as well.
@techdose4u
@techdose4u 4 жыл бұрын
System design won't be possible for this season of placements. I will put it before 2021 placements.
@aniketpandey1913
@aniketpandey1913 3 жыл бұрын
@@techdose4u best thing about you is that you don't take hours to explain a problem, you do it within minutes and try to keep the video as short as possible exactly what most people prefer
@rahulchaubey8988
@rahulchaubey8988 4 жыл бұрын
Bro i have watched all your playlist. And not a single Q which i don't understand. And that too flawlessly.. Thanks for ur help broo.
@techdose4u
@techdose4u 4 жыл бұрын
Welcome :)
@hortsss
@hortsss 4 жыл бұрын
Same! And everytime I see something new, I try to apply to further problems because it's always very useful
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@willturner3440
@willturner3440 3 жыл бұрын
I code myself after watching explanation.. God bless you 🥰
@techdose4u
@techdose4u 3 жыл бұрын
Thanks :)
@Alikhan1993
@Alikhan1993 2 жыл бұрын
Small refactor. You don't need to pass rows and columns of the array in the dfs function as argument because they are constants and will never change. You can use image.length and image[0].length always to evaluate row and column inside the dfs function. Does not make sense to pass them always along the recursive calls because their data will never change and remain constant
@Brijeshkumar-lk5mt
@Brijeshkumar-lk5mt 4 жыл бұрын
This video was excellent, but sir the space complexity is not going to be O(1). It will be O(M*N) because of the call stack.
@vishwassahu
@vishwassahu 2 жыл бұрын
KZbin's most quality content
@techdose4u
@techdose4u 2 жыл бұрын
Thanks :)
@md_aaqil8027
@md_aaqil8027 4 жыл бұрын
Excellent explanation I like your video because precise ,easy to understand and clear explanation Keep rocking sir🥳🥳
@techdose4u
@techdose4u 4 жыл бұрын
Thanks bro :)
@asitkumar3164
@asitkumar3164 2 жыл бұрын
Code Quality Bhaiya ka : The Best, literally,... The Best 🔥🔥🔥
@vijayabiradar687
@vijayabiradar687 3 жыл бұрын
Easy to understand and clean code. Thanks for the video!
@techdose4u
@techdose4u 3 жыл бұрын
Welcome
@dhir_tech
@dhir_tech 2 жыл бұрын
one of the best code and explanation I ever seen , 100 time thanks bhaiya
@meghamalviya8495
@meghamalviya8495 4 жыл бұрын
Great Explanation!...like your approach for explaining the problem..thanks.
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@jainanish94
@jainanish94 4 жыл бұрын
Bro! You are doing great job. Just a suggestion thou, jasie jo bhi question hai na, like this one is from leetcode, then do include link of the question in description as well. Anyways Keep up the good work!
@techdose4u
@techdose4u 4 жыл бұрын
I already gave the leetcode problem number. So it should be easy to find.
@ethanhunt3671
@ethanhunt3671 2 жыл бұрын
Sir we can do it using BFS also (similar to rotten Oranges). In DFS auxiliary stack space was O(m*n) and in BFS also space complexity is O(m*n) [for using Queue]
@yashpaunikar671
@yashpaunikar671 3 ай бұрын
underrated but undefeated❤‍🔥❤‍🔥
@kunalsoni7681
@kunalsoni7681 4 жыл бұрын
I never understand this problem.. But after watching this video 😊 understand a lot..
@techdose4u
@techdose4u 4 жыл бұрын
😅
@binitkumarsingh8296
@binitkumarsingh8296 4 жыл бұрын
Great work bro ...u are helping a lot of programmers to understand the crux of the code,just a suggestion please make a video on using c++ stl in competitive programming.
@techdose4u
@techdose4u 4 жыл бұрын
I have not started videos on competitive programming yet. This placement season I will focus on graph, dp, heap etc. After placement materials are ready, I will start CP as well.
@suhanshupatel9204
@suhanshupatel9204 4 жыл бұрын
underated channel... what a vedio:)
@techdose4u
@techdose4u 4 жыл бұрын
Thanks :)
@MonirsOfficial
@MonirsOfficial 7 ай бұрын
Great Explanation Bro..
@arthamsreenivas8858
@arthamsreenivas8858 4 жыл бұрын
Nice Explanation, but i did not understand the leet code problem statement even though i know DFS pattern
@techdose4u
@techdose4u 4 жыл бұрын
😅 why? The question was clearly explained with example in leetcode.
@rajeshseptember09
@rajeshseptember09 2 жыл бұрын
hey awesome video. i solved this question in exactly the same way without knowing how this could be DFS. Could you please explain ? for me, it's just plain recursion being called in all directions and then the base case.
@FaisalSaifi
@FaisalSaifi Жыл бұрын
DFS is the technique where you keep traversing a single branch until there is none left, then only traverse back to do the same for every node in that branch. That is why it is called depth first search. Similarly, here we are traversing till we cannot find any adjacent colors that are the same.
@dhanashreegodase4445
@dhanashreegodase4445 3 жыл бұрын
Excellent explanation .....................
@techdose4u
@techdose4u 3 жыл бұрын
Thanks
@TheMcallist1
@TheMcallist1 3 жыл бұрын
Amazing video, thanks for making
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@sundaypodcom
@sundaypodcom Жыл бұрын
Good stuff
@elvissam1401
@elvissam1401 2 жыл бұрын
Your videos are really helpful, thanks for sharing this content
@shreelakshmi6890
@shreelakshmi6890 3 жыл бұрын
thanks for the clear explanation
@techdose4u
@techdose4u 3 жыл бұрын
Welcome :)
@freshcontent3729
@freshcontent3729 3 жыл бұрын
this is DFS but he did not use stack here .. then how is it DFS ?
@amruthaj4262
@amruthaj4262 7 ай бұрын
Why is the new color 3 in the 2nd example?
@NikhilKumar-oy7mx
@NikhilKumar-oy7mx 4 жыл бұрын
i remember exact same kind of question and the solution last month.
@NikhilKumar-oy7mx
@NikhilKumar-oy7mx 4 жыл бұрын
i mean similar
@NikhilKumar-oy7mx
@NikhilKumar-oy7mx 4 жыл бұрын
remembering your previous solution i submitted this class Solution { public: void color(vector< vector>& image, int sr,int sc,int source,int newcolor) { if(sr=image[0].size()) return; else if(image[sr][sc]!=source) return; image[sr][sc]=newcolor; color(image,sr-1,sc,source,newcolor); color(image,sr+1,sc,source,newcolor); color(image,sr,sc-1,source,newcolor); color(image,sr,sc+1,source,newcolor); } vector floodFill(vector& image, int sr, int sc, int newColor) { if(image[sr][sc]==newColor) return image; int source=image[sr][sc]; color(image,sr,sc,source,newColor); return image; } }; Thanks a lot for your videos.
@pujaridevanjagadeesan2170
@pujaridevanjagadeesan2170 4 жыл бұрын
Got this for google
@agileprogramming7463
@agileprogramming7463 4 жыл бұрын
Yes the number of islands question from the april challenge
@techdose4u
@techdose4u 4 жыл бұрын
Nice :)
@anirudhv0062
@anirudhv0062 5 ай бұрын
I wanted to ask. Does the sequence of directions we move matter? I watched the video to understand the solution after spending hours on it. In the video the filling was done top->right->down->left . But in the code, the recursion statements were applied top, down, left, right. I am confused. Can someone clarify if the order matters here?
@nobita7231
@nobita7231 4 жыл бұрын
Brother how do calculate time complexity and space complexity??
@techdose4u
@techdose4u 4 жыл бұрын
I have made some basic videos on time and space analysis. Please watch them.
@amotekundev
@amotekundev 11 ай бұрын
Thank you
@nishantshah_
@nishantshah_ Жыл бұрын
Love your explanation, thanks !!
@surajmaity6194
@surajmaity6194 2 жыл бұрын
Thanks for the video!
@jaydeepmahajan6598
@jaydeepmahajan6598 4 жыл бұрын
Hey in your playlist section in may challenge it is showing 17 videos but till now you just created 12 , some of them are showing more than once , so just checkout
@techdose4u
@techdose4u 4 жыл бұрын
I don't know why this is happening everytime. I will correct it. Thanks for pointing.
@dipeshdarji6253
@dipeshdarji6253 4 жыл бұрын
you haven't use the visited matrix in order to mark visited cells. I don't know but can you tell me that does your code gives RTE?
@safalyaghoshroy2405
@safalyaghoshroy2405 4 жыл бұрын
Exactly bro
@techdose4u
@techdose4u 4 жыл бұрын
RTE means?
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
No it should not give RTE... In my first attempt, I didn't check whether the source is already == new color... If already equal then simply return, no need to do dfs. If we don't check this condition then it will give RTE
@sarbojitsarkar1226
@sarbojitsarkar1226 4 жыл бұрын
@@techdose4u RTE : Run time error, I guess
@dipeshdarji6253
@dipeshdarji6253 4 жыл бұрын
@@techdose4u Run Time Error
@andrewchen861
@andrewchen861 Жыл бұрын
Thanks!
@safalyaghoshroy2405
@safalyaghoshroy2405 4 жыл бұрын
First i have codeed this but it is giving WA but taking a visited array passes.
@techdose4u
@techdose4u 4 жыл бұрын
Yes it will pass.
@awakashsinha9926
@awakashsinha9926 4 жыл бұрын
I did the same but didn't pass col and row separately and got WA, why so?
@jazlynlin9995
@jazlynlin9995 Жыл бұрын
wrong space complexity - recursion involves call stacks which are impossible to be O(1)
@chandrakantgarg1069
@chandrakantgarg1069 3 жыл бұрын
Can we also solve it using BFS ? If yes , than does the time and space complexity remains the same ?
@amitavamozumder73
@amitavamozumder73 3 жыл бұрын
yes, we can do bfs as well, size will be reduced i guess since we're not pushing the entire function call stack we're just pushing the x,y coordinates in the queue.
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@@amitavamozumder73 in video what he did is DFS right ? but he did not use stack here .. then how is it DFS ?
@amitavamozumder73
@amitavamozumder73 3 жыл бұрын
@@freshcontent3729 the recursion uses the call stack of the OS, calling same function again is push, returning from a function is pop from stack.
@freshcontent3729
@freshcontent3729 3 жыл бұрын
@@amitavamozumder73 thanks, if asked this same problem in interview which one should I start with .. the BFS approach or the DFS approcach ?
@amitavamozumder73
@amitavamozumder73 3 жыл бұрын
@@freshcontent3729 whichever you think is easy for you to understand and implement. practice both types of dfs with stack and with recursion, there may be scenarios where bfs is not possible. (some graph algos)
@victorvondoom2350
@victorvondoom2350 2 жыл бұрын
the code is similar to the minesweeper game i coded . only realised the algorithm for minesweeper is flood fill.
@akhilnarayanan7182
@akhilnarayanan7182 3 жыл бұрын
Which mic do you use?
@rajeshbammidy180
@rajeshbammidy180 4 жыл бұрын
Java Soln using BFS:github.com/RajeshAatrayan/InterviewPreparation/blob/master/src/Graphs/FloodFill.java
@techdose4u
@techdose4u 4 жыл бұрын
Thanks for sharing
@Meethu69
@Meethu69 3 жыл бұрын
why can't we use bfs?
@saunaknandi1814
@saunaknandi1814 2 жыл бұрын
I dont think the code will work for 0 1 matrix and newColor = 1 like {{0,0,0},{0,1,1}} and sr=1 sc=1
@programmingstuff5741
@programmingstuff5741 4 жыл бұрын
You have not made videos on GOOGLE kickstart solution as you have said promise before
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
Bro, he is having too much office works, that's why he is not able to make videos. There are many topics which he also wants to make but couldn't
@spetsnaz_2
@spetsnaz_2 4 жыл бұрын
We can simply wait for him to make videos
@techdose4u
@techdose4u 4 жыл бұрын
I can understand that everyone needs more and more videos. But think about me as well. It is not possible to make more than one video a day. I am not working full time on KZbin.
@md_aaqil8027
@md_aaqil8027 4 жыл бұрын
Awesome video Practically it's not possible to make more than one video per day with office work I agree with your comments sir Do you suggest any other resources or KZbin channel to learn more questions like your video explanation sir?
@techdose4u
@techdose4u 4 жыл бұрын
Actually there were some videos from Gaurav sen and he used to post them. But currently he is not making regular videos. I would recommend Errichto for Competitive programming. He is awesome but I don't know if you all can understand hi accent and way of teaching. Rest all are just doing marketing on KZbin for their gains and selling their courses. I would recommend Errichto only. He is an excellent coder.
@_abhishekmj_
@_abhishekmj_ 3 жыл бұрын
Can we do this with BFS?
@ИкромХасанбаев
@ИкромХасанбаев 2 жыл бұрын
what program do you draw with?
@amittiwari360
@amittiwari360 3 жыл бұрын
Can we use bfs here ?
@fatimajaffal9843
@fatimajaffal9843 4 жыл бұрын
class Solution { public: vector floodFill(vector& image, int sr, int sc, int newColor) { int prevVal = image[sr][sc]; image[sr][sc] = newColor; if(prevVal == newColor) return image; if(sc-1 >= 0 && image[sr][sc-1] == prevVal) floodFill(image,sr,sc-1,newColor); if(sc+1 < image[0].size() && image[sr][sc+1] == prevVal) floodFill(image,sr,sc+1,newColor); if(sr-1 >= 0 && image[sr-1][sc] == prevVal) floodFill(image,sr-1,sc,newColor); if(sr+1 < image.size() && image[sr+1][sc] == prevVal) floodFill(image,sr+1,sc,newColor); return image; } };
@techdose4u
@techdose4u 4 жыл бұрын
Have you done this in the original function itself? Looks messy 😅 It's always better to use another function for recursion.
@pratiksarvan
@pratiksarvan 3 жыл бұрын
What’s the software you using to explain ?
@freshcontent3729
@freshcontent3729 3 жыл бұрын
his is DFS but he did not use stack here .. then how is it DFS ?
@boomer_mcghee
@boomer_mcghee 3 ай бұрын
i love you
@techdose4u
@techdose4u 3 ай бұрын
love you too
@CostaKazistov
@CostaKazistov 2 жыл бұрын
Kotlin class Solution { lateinit var rows: IntRange lateinit var cols: IntRange fun floodFill(image: Array, sr: Int, sc: Int, color: Int): Array { if (image[sr][sc] == color) return image rows = image.indices cols = image.first().indices val sourceColour = image[sr][sc] dfs(sr, sc, sourceColour, color, image) return image } fun dfs(row: Int, col: Int, sourceColor: Int, color: Int, image: Array) { if (row in rows && col in cols && image[row][col] == sourceColor) { image[row][col] = color dfs(row + 1, col, sourceColor, color, image) dfs(row - 1, col, sourceColor, color, image) dfs(row, col + 1, sourceColor, color, image) dfs(row, col - 1, sourceColor, color, image) } } }
G-9. Flood Fill Algorithm | C++ | Java
20:34
take U forward
Рет қаралды 244 М.
Flood Fill - LeetCode 733 - JavaScript
7:23
AlgoJS
Рет қаралды 1,2 М.
The selfish The Joker was taught a lesson by Officer Rabbit. #funny #supersiblings
00:12
когда не обедаешь в школе // EVA mash
00:51
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 243 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 568 М.
Excel Sheet Column Title - Leetcode 168 - Python
11:50
NeetCodeIO
Рет қаралды 13 М.
LeetCode 733. Flood Fill (Algorithm Explained)
10:03
Nick White
Рет қаралды 57 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 658 М.
Maximal square | Dynamic programming | Leetcode #221
18:27
Techdose
Рет қаралды 71 М.
Flood Fill
7:42
Kevin Naughton Jr.
Рет қаралды 33 М.
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 106 М.
Algorithms Explained for Beginners - How I Wish I Was Taught
17:38
Internet Made Coder
Рет қаралды 352 М.
This is how Paint's bucket fill works (Flood fill algorithm)
8:54
The selfish The Joker was taught a lesson by Officer Rabbit. #funny #supersiblings
00:12