How To Solve Graph Matrix Problems using DFS | Leetcode Flood Fill | CP Course | EP 72

Поделиться
HTML-код
  • Опубликовано: 24 дек 2024

Комментарии • 299

  • @zishan53
    @zishan53 2 года назад +9

    Nice ,
    I follow your course from 3-4month before from this Feb 21/2022 and honestly I can understand how to approach and how to think such type of questions ......
    Maza hi rha hai but pure tarike se abhi Sikh rha hu...
    Thanku luv 💯💯

  • @asishpanda3547
    @asishpanda3547 2 года назад +3

    This is whats makes u different and unique from others 8:53 , thanks man i got what i came for

  • @iampranshu
    @iampranshu Год назад +5

    💛 loved it, can't be explained more beautifully!!

  • @adityamaurya3673
    @adityamaurya3673 3 года назад +15

    Try to solve island perimeter question from leetcode after understanding this problem where you have to take care of boundaries of your traversal only---- i tried by myself just click me the approach and i submitted successfully.

    • @Yuvraj20011
      @Yuvraj20011 3 года назад

      bhai code bhejdo mujhe error aa rha hai shayad recursion wale part me

    • @aniketash4738
      @aniketash4738 3 года назад +3

      @@Yuvraj20011
      int cnt=0;
      void dfs(int i , int j, vector&graph)
      {
      int n=graph.size();
      int m= graph[0].size();
      if(i=m)return;
      if(graph[i][j]!=1)return;
      if(i-1=m)cnt++;
      if(i-1>=0 && graph[i-1][j]==0)cnt++;//one side water
      if(j-1>=0 && graph[i][j-1]==0)cnt++;//one side water
      if(i+1

    • @Progamer-fq8st
      @Progamer-fq8st 2 года назад

      @@Yuvraj20011 class Solution {
      public:

      int dfs(int i, int j, vector& grid, vector & vis){
      int count = 0;

      int n = grid.size();
      int m= grid[0].size();
      if(i=m){
      return 1;
      }
      if(grid[i][j] == 0){
      return 1;
      }
      if(vis[i][j])return 0;


      vis[i][j] = 1;
      count += dfs(i+1, j, grid, vis);
      count += dfs(i-1, j, grid, vis);
      count += dfs(i, j+1, grid, vis);
      count += dfs(i, j-1, grid, vis);

      return count;
      }

      int islandPerimeter(vector& grid) {
      vector vis;
      int n = grid.size();
      int m= grid[0].size();
      for(int i = 0; i

  • @rishabhverma7730
    @rishabhverma7730 3 года назад +13

    We haven't used the visited as visited is used to prevent the initial recursion calls, but in this we are changing the values of image itself that will do the same work as visited and helps in prevention of initial recursion to be get called again.
    Edited for second question on every dfs calls just do count++ ..... And return the count.

    • @ENGCS_JaiSaxena
      @ENGCS_JaiSaxena 2 года назад +1

      maybe u have nt tried to submit the code.
      [[0,0,0],[0,0,0]]
      0
      0
      0 try this case. it wont work without visited array.

    • @rohan_2844
      @rohan_2844 2 года назад

      @@ENGCS_JaiSaxena In main loop, we already declared that if new color equals initial color, we wont run dfs, so no use of visited array.

  • @Red___Beast
    @Red___Beast 10 месяцев назад +1

    wah luv sir, island vala khud kar liya , thank you bhaiya

  • @RAHULYADAV-zr5fq
    @RAHULYADAV-zr5fq 2 года назад +2

    I will always be very very grateful to u bhai!! Ur explanation is really great.

  • @shravankumaruppala8457
    @shravankumaruppala8457 4 месяца назад

    💛 understood 👍
    I tried the visited array technique for second question (no.of islands) it worked fine with some little tweks of previous question code👍

  • @minikin_pixie
    @minikin_pixie 2 года назад +1

    Your videos are too good ! Awesome !!

  • @ChandraShekhar-by3cd
    @ChandraShekhar-by3cd 3 года назад +8

    @Luv . Pls make some playlist on Leetcode Daily Challenges. It will be very much helpful for the Interview preparation for Everyone. Thanks

  • @shrutirani8181
    @shrutirani8181 Год назад

    💛 sir learners missing u ....great explanation

  • @sumanmandal5952
    @sumanmandal5952 2 года назад

    Jahapana tusi great ho , taufa kabool karo 🙇🏻🙇🏻

  • @sejalrai30
    @sejalrai30 2 года назад +1

    thank you so so much..ab jaake cheeze smjh aarahi h

  • @srinivaschaitanya3305
    @srinivaschaitanya3305 Год назад +1

    in dfs , generally after every recursion call, the state of the final result keeps changing.in order to maintain the state , we backtrack. Here there is no need once we change the color . So visited array is not used

  • @omarfaruk538
    @omarfaruk538 2 года назад

    Love you brother 😚
    Your each video makes me happy 🙂

  • @funshowsdh1yl
    @funshowsdh1yl 2 года назад

    Very well explained, keep growing brother!! 💛

  • @rohan_2844
    @rohan_2844 2 года назад

    13:55 Visited not used because color will already be changes and then if It will go back then it would no longer be a valid edge.

  • @anmolshrivastava628
    @anmolshrivastava628 2 года назад

    💛 mind blowing explanation!!!

  • @ragibshahrier
    @ragibshahrier 6 месяцев назад

    💛 Excellent Explanation

  • @vanshikasingh2859
    @vanshikasingh2859 Год назад

    Amazing explanation!!

  • @amisha2545
    @amisha2545 3 года назад

    Explained so well! 👏

  • @sabbysays
    @sabbysays 3 года назад

    Coding with Luv is Love 💙💙💙

  • @rajneeshmishra6969
    @rajneeshmishra6969 5 месяцев назад

    💛 Loved it

  • @aarihant2001
    @aarihant2001 Год назад

    💛 luv your content

  • @codingwithmanish
    @codingwithmanish 2 года назад

    Explained so well

  • @MDFARHAN-xu5cl
    @MDFARHAN-xu5cl 2 года назад

    Thanks for such a great explanation.

  • @meetamin7343
    @meetamin7343 2 года назад

    Great Explanation💛💛

  • @sahilanand30
    @sahilanand30 3 года назад +1

    Best explaination :)

  • @aadityasharma9790
    @aadityasharma9790 3 года назад +3

    i didnt find yellow heart bt i watch ur vdo completely

  • @de_ansh
    @de_ansh 3 года назад +1

    Thank you so bhaiya for your efforts 🥰

  • @aradhyagupta2810
    @aradhyagupta2810 Год назад

    very good lecture

  • @vivekvatsalya8836
    @vivekvatsalya8836 Год назад

    you are making it damn easy

  • @ENGCS_JaiSaxena
    @ENGCS_JaiSaxena 2 года назад

    wonderful explanation;

  • @TheUTrader
    @TheUTrader 3 года назад +7

    First comment ❤️

  • @siddharthsrivastava7492
    @siddharthsrivastava7492 Год назад

    awesome explanation

  • @ahmadishtiyaque
    @ahmadishtiyaque 2 года назад

    Very interesting 💚💚

  • @abhaypratap9898
    @abhaypratap9898 3 года назад +1

    Love you bhaiya 💗

  • @e.m.hasanprince4609
    @e.m.hasanprince4609 3 года назад +4

    bro i think u should make a video on how to traverse a 2d graph and find some path with dfs. this topic seems quite hard for us.

  • @manojsharma-ox1kb
    @manojsharma-ox1kb 11 месяцев назад

    Thank you 💛

  • @princekoshti7245
    @princekoshti7245 Год назад

    Bhaiya i just thought about checking the condition of being initial color equal to new color and get the idea why visited is not used ....

  • @cosmic_coder
    @cosmic_coder 6 месяцев назад

    visited array may not be required because we do coloring if it is colored with new color then dfs not called

  • @kamalacharya8525
    @kamalacharya8525 3 года назад

    thank you bhaiya for this playlist

  • @kushall6819
    @kushall6819 Год назад

    island perimeter question which is mentioned in the description is not a graph question....right?

  • @anexocelisia9377
    @anexocelisia9377 2 года назад

    In the flood fill question, it is not required to use the visited thing as we are already using the color as a sign of vis. So it would be not useful for using the visited thing separately while performing dfs.

    • @lakshyasharma7448
      @lakshyasharma7448 2 года назад

      I thought of this too, but I am not sure whether this is right or not. Could you drop your solution here with this approach so I could take a look. Thanks

    • @HarshKumar-tf8mh
      @HarshKumar-tf8mh 2 года назад

      @@lakshyasharma7448 so you need the sol, huh

  • @nawabkhan4916
    @nawabkhan4916 3 года назад +1

    Yellow heart 👍👍

  • @abcefg7045
    @abcefg7045 3 года назад

    visited not required because just after checking whether the cell is changeable we changed its color and so if in the next case it will return as it will not meet the case color[i][j] != inicolor

  • @goyaldeekshant
    @goyaldeekshant 7 месяцев назад

    very nice

  • @DeepakVerma-iitbhu
    @DeepakVerma-iitbhu 3 года назад

    💛 thanks bhaiyya

  • @amanrubey
    @amanrubey 2 года назад

    Luv Sir Op 🥳

  • @malkeetsapien4852
    @malkeetsapien4852 2 года назад +1

    🐺 🤝 Thankx Bhai !

  • @jinniesrego
    @jinniesrego 3 года назад +3

    How do we knw when to use DFS and when to use BFS coz sometimes its confusing??

    • @iamluv
      @iamluv  3 года назад +7

      Bfs is used in very specific cases only which I will explain in future videos; rest must of the stuff done using dis can be done using bfs as well; so mostly you will always use dis only until and unless any special case arrives where only bfs works..

    • @jinniesrego
      @jinniesrego 3 года назад

      @@iamluv Thanks a lot

    • @aayush5474
      @aayush5474 3 года назад

      BFS is usually better than dfs because it does not involve recursive calls and explores level by level

  • @shadowmonarch3155
    @shadowmonarch3155 2 года назад

    is this same algorithm used in Chain reaction game??

  • @rahulchandra2568
    @rahulchandra2568 3 года назад

    Best❤️

  • @akshitsharma9488
    @akshitsharma9488 2 года назад

    Thanks Bhaiya

  • @jugal.suthar
    @jugal.suthar 3 года назад

    wow🔥🔥

  • @kevalvekariya2482
    @kevalvekariya2482 2 года назад

    💛 for you

  • @sahilchhabra2391
    @sahilchhabra2391 Год назад

    💛 doneee

  • @aledutron
    @aledutron 3 года назад

    ❤Thank You❤

  • @satanshuag
    @satanshuag 3 года назад

    Sir please Explain Covishield vs Covaxsin Codechef Problem

  • @naajidsarosh2881
    @naajidsarosh2881 3 года назад

    Bro, could you please tell how can we avoid the use of visited array in undirected graphs ?

    • @iamluv
      @iamluv  3 года назад

      in graphs you cannot, in trees you can

    • @naajidsarosh2881
      @naajidsarosh2881 3 года назад

      @@iamluv Will you tell in your upcoming videos how to do so like you said ?

  • @VinodKumar-bz9mg
    @VinodKumar-bz9mg 2 года назад +1

    I was able to solve the no of island question with your trainings. Thanks a lot
    class Solution {
    public static boolean[][] visited;
    void DFS(int i, int j, int m, int n, char[][] grid) {
    if (i < 0 || j < 0) return;
    if (i >= m || j >= n) return;
    if (visited[i][j]) return;
    if (grid[i][j] == '0') return;
    visited[i][j] = true;
    DFS(i, j - 1, m, n, grid);
    DFS(i, j + 1, m, n, grid);
    DFS(i - 1, j, m, n, grid);
    DFS(i + 1, j, m, n, grid);
    }
    public int numIslands(char[][] grid) {
    int noi = 0;
    int m = grid.length;
    int n = grid[0].length;
    visited = new boolean[m][n];
    for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
    if (grid[i][j] != '0' && !visited[i][j]) {
    noi++;
    DFS(i, j, m, n, grid);
    }
    }
    }
    return noi;
    }
    }

  • @sikkukumar6675
    @sikkukumar6675 2 года назад

    mast

  • @shivasaini4097
    @shivasaini4097 3 года назад

    sorry bhaiya video dekhne m thora late ho gaya ❤

  • @codeblooded6760
    @codeblooded6760 2 года назад +1

    Some companies will ask you why did you use dfs instead of bfs? What to answer here?

  • @TrideepBanerjee_
    @TrideepBanerjee_ 2 года назад

    Solved the number of islands question after watching your video but its showing seg fault can anyone help?
    The code
    void dfs(int i,int j,vector& grid,vector &vis)
    {
    int r=grid.size();
    int c=grid[0].size();
    if(ic) return;
    if(grid[i][j]=='0') return;
    if(vis[i][j]) return;
    vis[i][j]=true;
    dfs(i+1,j,grid,vis);
    dfs(i-1,j,grid,vis);
    dfs(i,j+1,grid,vis);
    dfs(i,j-1,grid,vis);
    dfs(i+1,j+1,grid,vis);
    dfs(i+1,j-1,grid,vis);
    dfs(i-1,j+1,grid,vis);
    dfs(i-1,j-1,grid,vis);
    }
    int numIslands(vector& grid) {
    // Code here
    vector vis;
    int cnt=0;
    for(int i=0;i

  • @leninsingh5264
    @leninsingh5264 2 года назад

    How to do if it is diagonally visiting?

  • @yashmandaviya1356
    @yashmandaviya1356 3 года назад

    flood fill problem is also known as the rotten tomatoes problem , right?

  • @anonymousfan9703
    @anonymousfan9703 3 года назад

    Tak nice

  • @anowlwithinternet9125
    @anowlwithinternet9125 2 года назад +1

    💛💛💛💛

  • @nileshchaudhary1643
    @nileshchaudhary1643 3 года назад +1

    💛💛💛

  • @funenjoynilaypatel4553
    @funenjoynilaypatel4553 2 года назад +1

    8-aug 2022

  • @harshgahlot2555
    @harshgahlot2555 4 месяца назад

    Find my solution below
    class Solution {
    public:
    void dfs(int i, int j, int n, int m, vector &vis, vector &grid)
    {
    //condition checks on the entering node
    if(i=m) return;

    if(grid[i][j]=='0')
    {
    vis[i][j]=1;
    return;
    }
    if(vis[i][j]==1) return;
    //operation on current node
    vis[i][j]=1;
    //apply dfs on the child nodes on all four directions '
    dfs(i,j+1,n,m,vis,grid);
    dfs(i,j-1,n,m,vis,grid);
    dfs(i+1,j,n,m,vis,grid);
    dfs(i-1,j,n,m,vis,grid);
    }
    int numIslands(vector& grid)
    {
    //cout

  • @akshaya61
    @akshaya61 Год назад

    class Solution {
    public:
    void dfs(int i,int j,vector& grid){
    int n=grid.size();
    int m=grid[0].size();
    if(i=m)return;
    if(grid[i][j]!='1')return;
    grid[i][j]='0';
    dfs(i-1,j,grid);
    dfs(i+1,j,grid);
    dfs(i,j-1,grid);
    dfs(i,j+1,grid);
    }
    int numIslands(vector& grid) {
    int cnt=0;
    for(int i=0;i

  • @princekumar-wt7ne
    @princekumar-wt7ne 2 года назад

    Red heart chhodenge ❤️

  • @nishantraj9762
    @nishantraj9762 7 месяцев назад

    Background music name

  • @ENGCS_JaiSaxena
    @ENGCS_JaiSaxena 2 года назад

    good explanation, but this code wont run for
    [[0,0,0],[0,0,0]]
    0
    0
    0
    it will enter , infinite loop so we have to use visited.

    • @udaykumarchetla6205
      @udaykumarchetla6205 2 года назад

      Bro, he has already included a condition in main function, where initial color is equal to final color. So we'll not run dfs for this case.

    • @ENGCS_JaiSaxena
      @ENGCS_JaiSaxena 2 года назад

      Then solve the above test case bro it wont get pass.
      Just dont say without even submitting the problem

    • @arpitgupta2358
      @arpitgupta2358 2 года назад

      @@ENGCS_JaiSaxena do u get the answer?

    • @ENGCS_JaiSaxena
      @ENGCS_JaiSaxena 2 года назад

      @@arpitgupta2358 what??

    • @arpitgupta2358
      @arpitgupta2358 2 года назад

      @@ENGCS_JaiSaxena [[0,0,0],[0,0,0]] iss test case ke liye aagya answer tumhara?

  • @ani68
    @ani68 3 года назад +1

    💛

  • @saquibalam7871
    @saquibalam7871 Год назад

    Bhaiya main function ka code q nhi dikhate???

  • @khancse999
    @khancse999 9 месяцев назад

    6:53

  • @__VishalSharma
    @__VishalSharma 2 года назад

    💛
    Bro iss code kya problem hai
    class Solution {
    public:
    void dfs(int i,int j,vector& grid){
    int n=grid.size();
    int m=grid[0].size();
    if(i=m) return;
    if(grid[i][j]!='1') return;
    dfs(i-1,j,grid);
    dfs(i+1,j,grid);
    dfs(i,j-1,grid);
    dfs(i,j+1,grid);

    }
    int numIslands(vector& grid) {
    int counter=0;
    for(int i=0;i

    • @yath3681
      @yath3681 2 года назад

      set grid[i][j] = '0' too after if() statements in dfs function
      this way you won't go back to the nodes which you have already visited

  • @chiraggoyal9646
    @chiraggoyal9646 2 года назад +1

    ♥️

  • @unknown_coder7960
    @unknown_coder7960 3 года назад +1

    Bhaiya Leetcode p 400 questions kr liye ab Elon Musk ko hara skta hu 🙄

  • @maddycoder1294
    @maddycoder1294 2 года назад

  • @funnyvideosAdda_
    @funnyvideosAdda_ 2 года назад

    💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛💛

  • @ashutoshkumarchoudhary6420
    @ashutoshkumarchoudhary6420 2 года назад

    Yellow heart

  • @ayushshukla2258
    @ayushshukla2258 2 года назад

    change color to yellow

  • @amanyadav6607
    @amanyadav6607 3 года назад +1

    💛

  • @nimeshsingh6229
    @nimeshsingh6229 3 года назад +1

    💛

  • @shouvik4679
    @shouvik4679 3 года назад

    💛💛

  • @jugal.suthar
    @jugal.suthar 3 года назад

    💛💛

  • @subhrajitdey2148
    @subhrajitdey2148 2 года назад

    💛

  • @nayeemislam8118
    @nayeemislam8118 2 года назад

    💛💛💛

  • @manojgollapelli9856
    @manojgollapelli9856 2 года назад +1

    💛💛

  • @vikrantbaranwal4391
    @vikrantbaranwal4391 2 года назад

    💛

  • @nehaagarwal350
    @nehaagarwal350 2 года назад +1

    💛💛

  • @priyabratamaji
    @priyabratamaji 2 года назад

    💛

  • @gauravupreti4398
    @gauravupreti4398 2 года назад

    💛

  • @manishasamanta9321
    @manishasamanta9321 2 года назад

    💛💛💛

  • @adarshjha9188
    @adarshjha9188 2 года назад

    💛

  • @hasleindia7027
    @hasleindia7027 2 года назад

    💛