Flood Fill | Leetcode

แชร์
ฝัง
  • เผยแพร่เมื่อ 10 พ.ย. 2024

ความคิดเห็น • 112

  • @rohitsrivastava5414
    @rohitsrivastava5414 4 ปีที่แล้ว +27

    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 ปีที่แล้ว +3

      System design won't be possible for this season of placements. I will put it before 2021 placements.

    • @aniketpandey1913
      @aniketpandey1913 3 ปีที่แล้ว +1

      @@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

  • @Alikhan1993
    @Alikhan1993 2 ปีที่แล้ว +1

    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

  • @rahulchaubey8988
    @rahulchaubey8988 4 ปีที่แล้ว +30

    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 ปีที่แล้ว +1

      Welcome :)

    • @hortsss
      @hortsss 4 ปีที่แล้ว +1

      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 ปีที่แล้ว +5

    I code myself after watching explanation.. God bless you 🥰

  • @asitkumar3164
    @asitkumar3164 2 ปีที่แล้ว +1

    Code Quality Bhaiya ka : The Best, literally,... The Best 🔥🔥🔥

  • @vishwassahu
    @vishwassahu 2 ปีที่แล้ว +2

    TH-cam's most quality content

  • @dhir_tech
    @dhir_tech 2 ปีที่แล้ว +1

    one of the best code and explanation I ever seen , 100 time thanks bhaiya

  • @kunalsoni7681
    @kunalsoni7681 4 ปีที่แล้ว +4

    I never understand this problem.. But after watching this video 😊 understand a lot..

  • @md_aaqil8027
    @md_aaqil8027 4 ปีที่แล้ว +6

    Excellent explanation
    I like your video because precise ,easy to understand and clear explanation
    Keep rocking sir🥳🥳

  • @vijayabiradar687
    @vijayabiradar687 4 ปีที่แล้ว +2

    Easy to understand and clean code.
    Thanks for the video!

  • @suhanshupatel9204
    @suhanshupatel9204 4 ปีที่แล้ว +2

    underated channel... what a vedio:)

  • @Brijeshkumar-lk5mt
    @Brijeshkumar-lk5mt 4 ปีที่แล้ว +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.

  • @yashpaunikar671
    @yashpaunikar671 5 หลายเดือนก่อน

    underrated but undefeated❤‍🔥❤‍🔥

  • @binitkumarsingh8296
    @binitkumarsingh8296 4 ปีที่แล้ว +3

    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 ปีที่แล้ว +1

      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.

  • @meghamalviya8495
    @meghamalviya8495 4 ปีที่แล้ว +2

    Great Explanation!...like your approach for explaining the problem..thanks.

  • @ethanhunt3671
    @ethanhunt3671 2 ปีที่แล้ว +1

    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]

  • @jainanish94
    @jainanish94 4 ปีที่แล้ว +3

    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.

  • @MonirsOfficial
    @MonirsOfficial 8 หลายเดือนก่อน

    Great Explanation Bro..

  • @rajeshbammidy180
    @rajeshbammidy180 4 ปีที่แล้ว +3

    Java Soln using BFS:github.com/RajeshAatrayan/InterviewPreparation/blob/master/src/Graphs/FloodFill.java

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      Thanks for sharing

  • @dhanashreegodase4445
    @dhanashreegodase4445 3 ปีที่แล้ว +1

    Excellent explanation .....................

  • @TheMcallist1
    @TheMcallist1 3 ปีที่แล้ว +2

    Amazing video, thanks for making

  • @sundaypodcom
    @sundaypodcom ปีที่แล้ว

    Good stuff

  • @surajmaity6194
    @surajmaity6194 2 ปีที่แล้ว

    Thanks for the video!

  • @anirudhv0062
    @anirudhv0062 6 หลายเดือนก่อน

    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?

  • @arthamsreenivas8858
    @arthamsreenivas8858 4 ปีที่แล้ว +3

    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.

  • @elvissam1401
    @elvissam1401 2 ปีที่แล้ว

    Your videos are really helpful, thanks for sharing this content

  • @nishantshah_
    @nishantshah_ ปีที่แล้ว

    Love your explanation, thanks !!

  • @NikhilKumar-oy7mx
    @NikhilKumar-oy7mx 4 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      Got this for google

    • @agileprogramming7463
      @agileprogramming7463 4 ปีที่แล้ว +3

      Yes the number of islands question from the april challenge

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Nice :)

  • @amruthaj4262
    @amruthaj4262 8 หลายเดือนก่อน

    Why is the new color 3 in the 2nd example?

  • @rajeshseptember09
    @rajeshseptember09 2 ปีที่แล้ว +3

    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.

  • @fatimajaffal9843
    @fatimajaffal9843 4 ปีที่แล้ว +1

    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.

  • @shreelakshmi6890
    @shreelakshmi6890 3 ปีที่แล้ว +2

    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 ?

  • @amotekundev
    @amotekundev ปีที่แล้ว

    Thank you

  • @nobita7231
    @nobita7231 4 ปีที่แล้ว +2

    Brother how do calculate time complexity and space complexity??

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      I have made some basic videos on time and space analysis. Please watch them.

  • @akhilnarayanan7182
    @akhilnarayanan7182 3 ปีที่แล้ว +1

    Which mic do you use?

  • @awakashsinha9926
    @awakashsinha9926 4 ปีที่แล้ว

    I did the same but didn't pass col and row separately and got WA, why so?

  • @dipeshdarji6253
    @dipeshdarji6253 4 ปีที่แล้ว +2

    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 ปีที่แล้ว +5

      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 ปีที่แล้ว +1

      @@techdose4u RTE : Run time error, I guess

    • @dipeshdarji6253
      @dipeshdarji6253 4 ปีที่แล้ว +1

      @@techdose4u Run Time Error

  • @chandrakantgarg1069
    @chandrakantgarg1069 3 ปีที่แล้ว +2

    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 ปีที่แล้ว +1

      @@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)

  • @jaydeepmahajan6598
    @jaydeepmahajan6598 4 ปีที่แล้ว +1

    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.

  • @andrewchen861
    @andrewchen861 ปีที่แล้ว

    Thanks!

  • @ИкромХасанбаев
    @ИкромХасанбаев 2 ปีที่แล้ว

    what program do you draw with?

  • @_abhishekmj_
    @_abhishekmj_ 3 ปีที่แล้ว

    Can we do this with BFS?

  • @safalyaghoshroy2405
    @safalyaghoshroy2405 4 ปีที่แล้ว +1

    First i have codeed this but it is giving WA but taking a visited array passes.

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว

      Yes it will pass.

  • @amittiwari360
    @amittiwari360 3 ปีที่แล้ว

    Can we use bfs here ?

  • @jazlynlin9995
    @jazlynlin9995 ปีที่แล้ว

    wrong space complexity - recursion involves call stacks which are impossible to be O(1)

  • @Meethu69
    @Meethu69 3 ปีที่แล้ว

    why can't we use bfs?

  • @victorvondoom2350
    @victorvondoom2350 2 ปีที่แล้ว

    the code is similar to the minesweeper game i coded . only realised the algorithm for minesweeper is flood fill.

  • @pratiksarvan
    @pratiksarvan 4 ปีที่แล้ว

    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 ?

  • @programmingstuff5741
    @programmingstuff5741 4 ปีที่แล้ว +1

    You have not made videos on GOOGLE kickstart solution as you have said promise before

    • @spetsnaz_2
      @spetsnaz_2 4 ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      We can simply wait for him to make videos

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +1

      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 TH-cam.

    • @md_aaqil8027
      @md_aaqil8027 4 ปีที่แล้ว +2

      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 TH-cam channel to learn more questions like your video explanation sir?

    • @techdose4u
      @techdose4u  4 ปีที่แล้ว +6

      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 TH-cam for their gains and selling their courses. I would recommend Errichto only. He is an excellent coder.

  • @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

  • @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)
    }
    }
    }

  • @boomer_mcghee
    @boomer_mcghee 4 หลายเดือนก่อน

    i love you

    • @techdose4u
      @techdose4u  4 หลายเดือนก่อน

      love you too