G-8. Number of Islands | Number of Connected Components in Matrix | C++ | Java

Поділитися
Вставка
  • Опубліковано 25 лис 2024

КОМЕНТАРІ • 487

  • @takeUforward
    @takeUforward  2 роки тому +90

    In leetcode, the question is different. Read the movements there.
    Let's continue the habit of commenting “understood” if you got the entire video.
    Do follow me on Instagram: striver_79

    • @yash07k_n
      @yash07k_n 2 роки тому +3

      Striver, video description me " C++/Java/Codes and Notes Link" galat hai...

    • @manojkr2362
      @manojkr2362 2 роки тому +1

      @@venkatesh.guntreddi i would say learn basics of loop ,for ,if else statement first and then move in to advance topices like stack ,queue etc . all the best and welcome to the coding community my brother

    • @Thescienceworld652
      @Thescienceworld652 Рік тому

      yes , only four diresctions are considered in it .

    • @prudhvirajmacherla9854
      @prudhvirajmacherla9854 Рік тому

      Understood

    • @PriyankaGupta-nb6wl
      @PriyankaGupta-nb6wl Рік тому

      yes, leetcode question is bit different, instead of 8 directions, 4 are considered.
      its code can be
      class Solution {
      public:
      void bfs(vector&vis,vector&grid,int i,int j)
      {
      vis[i][j]=1;
      int n=grid.size();
      int m=grid[0].size();
      queueq;
      q.push({i,j});
      while(!q.empty())
      {
      int row=q.front().first;
      int col=q.front().second;
      q.pop();
      /This part has to be changed
      int nrow=row-1;
      int ncol=col;
      if(ncol>=0 && ncol=0 && nrow=0 && ncol=0 && nrow=0 && ncol=0 && nrow=0 && ncol=0 && nrow

  • @sadiashafaque3517
    @sadiashafaque3517 Рік тому +42

    This is by far the best graph series ever found on internet, cant thank you enough for this!

  • @theskyspace8280
    @theskyspace8280 Рік тому +15

    I've been hoping from few playlists and books lately to get some confidence in this topic and your series just does that.
    Thanks tons.

  • @rahulreddyambati9065
    @rahulreddyambati9065 3 місяці тому +8

    Striver, I Thank You a lot ! I just had a similar question in which this was a part in my GOOGLE interview. Couldn't have done it without this. Thanks again :)

  • @rahulyadavKEC
    @rahulyadavKEC Рік тому +34

    For leetcode Problem... small change in the condition for the directions...
    class Solution {
    private:
    void bfs(vector& grid,int i,int j,vector&vis)
    {
    vis[i][j]=1;
    queueq;
    q.push({i,j});
    int row=grid.size();
    int col=grid[0].size();
    int delrow[4]={-1,0,1,0};
    int delcol[4]={0,1,0,-1};
    while(!q.empty())
    {
    int r=q.front().first;
    int c=q.front().second;
    q.pop();
    for(int k=0;k=0 && newrow=0 && newcol

  • @___divykant___
    @___divykant___ Рік тому +8

    you appplied the same for loops in 3D DP - when ALICE and BOB wanted to collect the choclates , just amazing how you teach and how we corelate ❤❤

  • @harshniteprasad5301
    @harshniteprasad5301 2 роки тому +14

    adding a pseudo code for four directional traversal namely up, down ,left and right.
    vector delta = {0,1,0,-1,0};
    for(int k =0;k

  • @UECSoumyaRay
    @UECSoumyaRay Рік тому +66

    The LeetCode and the GFG problem are vastly different. For starters, the LeetCode problem is only 4 dimensional and the GFG one is 8 dimensional(which is not a big deal, ofc). But the LeetCode problem has the concept of rotation which is different from the GFG problem. LeetCode problem is based on identification of "identical islands". GFG problem deals with identification of "connected islands"

    • @sahilbani7020
      @sahilbani7020 Рік тому +10

      thanks mate, I didn't read the leetcode problem
      there's a simple change to the if condition
      if(delrow==0 || delcol==0)

    • @anandchauhan1868
      @anandchauhan1868 Рік тому

      @@sahilbani7020 thanks bhai

    • @shiblijamal8544
      @shiblijamal8544 10 місяців тому

      Yes in leetcode u need to add the condition of abs value of i == abs j then continue it like that ...

    • @HitanshuSatpathy
      @HitanshuSatpathy 10 місяців тому

      bro its now in premium category...sad me

    • @vinaykumardiona
      @vinaykumardiona 10 місяців тому

      bro i wasted lot of time because of this bro. thank you so much...

  • @LightYagami406
    @LightYagami406 5 місяців тому +2

    for LeetCode the solution is slightly different since it only allows movement in 4 directions only
    The DFS code is as follows:
    class Solution {
    public:
    void dfs(int row, int col, vector&grid, vector &vis){
    int n = grid.size();
    int m = grid[0].size();
    vis[row][col] = 1;
    int delR[4]= {-1,0,1,0};
    int delC[4]= {0,1,0,-1};
    for(int i =0; i=0 && nRow=0 && nCol

  • @adityasood04
    @adityasood04 4 місяці тому +9

    For people who are confused in Leetcode problem:
    In leetcode, it is specified that we have to check only in 4 direction instead of 8 i.e. top, right, bottom and left.
    The same code will work, just you have to not consider the remaining 4 directions.
    You have to add this extra check in your loops:
    if((i==-1 && j==-1) || (i==-1&&j==1) || (i==1&&j==-1) || (i==1&&j==1)) continue;
    Updated for loops in bfs() function:
    for(int i=-1; i=0 && nbrCol < m && grid[nbrRow][nbrCol] == '1' && !vis[nbrRow][nbrCol]){
    vis[nbrRow][nbrCol] = 1;
    q.push({nbrRow, nbrCol});
    }
    }
    }

    • @nishantg7840
      @nishantg7840 3 місяці тому +2

      if((i==-1 && j==-1) || (i==-1&&j==1) || (i==1&&j==-1) || (i==1&&j==1)) continue;
      instead we can write if(i*j!=0) continue;

  • @nathonluke4058
    @nathonluke4058 2 роки тому +25

    Amazing 😍😍, I did it with DFS. But i have a small doubt. In numIsland function where you are first time calling the BFS first time. Can we put the grid [i][j] =='1' This would decrease the number of BFS calls? ..
    Edit: You already Corrected that😎😎.... Means i am on right path🔥🔥🔥

  • @kunalsaha9239
    @kunalsaha9239 2 роки тому +4

    Such a great explanation dada... for such a complex question ... took around 2 hrs to understand and code it down... but very well and clear explanation

  • @mayankjain0141
    @mayankjain0141 2 роки тому +6

    Space complexity for queue will be O(max(M,N))
    since bfs is a level order traversal and we start from the first most element, the maxm num elements will be the diagonal

  • @player-te8tf
    @player-te8tf 2 роки тому +25

    For the people suffering on leetcode, take a check just after that shortcut trick so that we dont check the diagonal elements as connected components -
    void bfs(int row,int col, vector &vis, vector &grid){
    vis[row][col]=1;
    queue q;
    int n = grid.size();
    int m = grid[0].size();
    q.push({row,col});
    while(!q.empty()){
    int row = q.front().first;
    int col = q.front().second;
    q.pop();
    //for neighbouring elements or cell.
    //you could use direct indexes too :)
    for(int del_row=-1;del_row

    • @sailendrachettri8521
      @sailendrachettri8521 2 роки тому +1

      Thanks !)

    • @codewith_ayushluthra5248
      @codewith_ayushluthra5248 2 роки тому +1

      Thanks

    • @indrajitruidas12
      @indrajitruidas12 2 роки тому +1

      Thanks :-)

    • @jonny._.1313
      @jonny._.1313 2 роки тому +1

      Intuition behind this shortcut please which is used to check all connected lands

    • @player-te8tf
      @player-te8tf 2 роки тому +2

      @@jonny._.1313evenif i will be explaining then it wont get clear to u untill u dry run the shortcut code itself......anyways...... But, you could even do it directly using indexes.

  • @Ace-ex9gg
    @Ace-ex9gg Рік тому +9

    you made java code a little complicated because i implemented in dfs and was way easier to understand. but anyway the explaination was realy good

  • @Zomb-zj4ip
    @Zomb-zj4ip 5 місяців тому

    for current leetcode question, an isalnd is considered as connected only through horizontal and vertical connections. to tackle this, simply skip the diagonal traversals while checking neighbours. to do this, inside the double nested for loop traversing delrow anddel col. include the first line as
    if(adjrow!=0 && adjcol!=0) continue;
    rest all remains exactly same.

  • @the_humble_lazy
    @the_humble_lazy 8 місяців тому +2

    the shortcut for visiting all the nodes was awesome

  • @AtharvaRao0104
    @AtharvaRao0104 2 місяці тому

    This grid can also be converted into a graph of adjacency list. But it may be time consuming in an interview. To do this, consider each box on the grid as a node. we need to number the nodes.
    So in above we can observe node 1 is connected to node 2, node 5, node6 . Node 0 and Node 1 are not connected. Node 0 and Node4 are also not connected. and so on ..
    But Grid problems are solved faster using the direction vectors to get the neighbours and referring to the node as a pair as shown in the video..
    Please watch William Fiset video on graph playlist related to grid. He has explained wonderfully.

  • @aryashjain7893
    @aryashjain7893 2 роки тому +9

    took two days but in the end "UNDERSTOOD" Thank U striver

    • @tg62672
      @tg62672 2 роки тому +7

      @@udaypratapsingh8923 not every one is smart as u

  • @shashank4577
    @shashank4577 2 роки тому +26

    Instead of creating another 2d array as visited, we can just mark the visited ones in the given array as 2. Or any number other than 1. Will save space :P . Just my 2 cents.

    • @valobhediya
      @valobhediya 2 роки тому

      can you share your code's link?

    • @luckygamers8068
      @luckygamers8068 2 роки тому +11

      We don't want to change the original matrix so we have to use extra matrix and see space is same as if we are doing dfs then stack space, or if we are doing via bfs then queue space..

    • @Harshanandita
      @Harshanandita Рік тому

      Bro, saving space should be in the algorithm, not that you change the input data structures, they're just meant to provide you the input...

    • @factsspitter7360
      @factsspitter7360 Рік тому +6

      It's a bad practice and cheating as some interviews might not allow you to modify inputs.

    • @anshuman8147
      @anshuman8147 8 місяців тому +1

      People say we should never modify the inputs

  • @ayushanand18
    @ayushanand18 Рік тому +2

    I really like his videos for the way he discusses time complexity.

  • @unknown2003august
    @unknown2003august 5 місяців тому +1

    understood so well , that even i can teach it anybody with full confidence , Thankyou Striver 😇

  • @a-talks4197
    @a-talks4197 2 роки тому +4

    Understood !! how easily you let us understand so much hard questions in a very easy manner :)

  • @nandiniverma5273
    @nandiniverma5273 2 роки тому +10

    Thankyou soo much for deep explanation and also for providing code in java , it really helps

  • @mehulpathak9845
    @mehulpathak9845 4 місяці тому +1

    Finally understood this problem completely . Thanks Striver.

  • @VikashKumardhor
    @VikashKumardhor 5 місяців тому +1

    instead of using visited matrix we can directly put replace that 1 with 0 reducing the space complexity

  • @xnehaxixh
    @xnehaxixh Рік тому +1

    The exact solution in python "TLE"s (10/26) 😵
    Welp, have to embrace the pain of coding in Python.
    Anyways, great to learn something new. Thanks!

  • @NARUTOUZUMAKI-bk4nx
    @NARUTOUZUMAKI-bk4nx 11 місяців тому +1

    Understood
    For leetcode problem only made this change
    for(int delrow=-1;delrow

  • @yashgarg4248
    @yashgarg4248 4 місяці тому

    I m touched as well with the dedication, you teach selflessly. ❤
    You are gem bro..

  • @sovankumardas5200
    @sovankumardas5200 8 місяців тому

    We can also define vector direction = [{0,1},{1,0},{0,-1},{-1,0},{-1,-1},{1,-1},{1,1},{-1,1}] then traverse using
    for(auto adj: direction){
    n_ row = row+adj.first;
    n_col = col +adj.second;
    }
    To get the all the neighbor of (row,col);
    Thanks Striver :)

  • @ayushigupta8633
    @ayushigupta8633 Рік тому +5

    Again I solved this problem by myself only after watching your explanation, Thank you striver for this amazing video 😁

  • @isheep9025
    @isheep9025 Рік тому +9

    Leet code c++ solution:
    class Solution {
    public:
    void solver(int i,int j,vector & grid,vector & visited)
    {
    int n=grid.size();
    int m=grid[0].size();
    queue q;
    q.push({i,j});
    visited[i][j]=1;
    while(!q.empty())
    {
    auto p=q.front();
    int row=p.first;
    int col=p.second;
    q.pop();
    int nr=row+1;
    int nc=col+0;
    if((nr>=0 && nr=0 && nc=0 && nr=0 && nc=0 && nr=0 && nc=0 && nr=0 && nc

    • @Codeitt
      @Codeitt Рік тому

      you are repeating the code instead use a function to check for adjacent "1".

  • @sakshamsharma648
    @sakshamsharma648 Рік тому +1

    Easy and Shortest one for this..........
    void DFS(vector& grid, int i, int j) {
    // boundary checking
    if(i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size())
    return;
    // return if current position is of water or is already visited
    if(grid[i][j] == '2' || grid[i][j] == '0')
    return;
    // mark the current as visited
    grid[i][j] = '2';
    // do DFS in all 4 directions
    DFS(grid, i+1, j);
    DFS(grid, i, j-1);
    DFS(grid, i-1, j);
    DFS(grid, i, j+1);
    }
    int numIslands(vector& grid) {
    // We can treat the matrix grid as a grid. Each Island is a
    // connected component. The task is to find no. of disconnectedd components
    // in the graph.
    int islands = 0;
    // We make each 1 as 2 in when it is visited
    for(int i = 0; i < grid.size(); i++) {
    for(int j = 0; j < grid[0].size(); j++) {
    // do DFS in case has not been visited and there is land
    if(grid[i][j] == '1') {
    DFS(grid, i, j);
    ++islands;
    }
    }
    }
    return islands;
    }

  • @urstrulyjatin
    @urstrulyjatin День тому

    Understood, thanks for you efforts.
    But for space optimisation, we can use the same grid matrix for visited. Maybe marking it with "-1".

  • @stith_pragya
    @stith_pragya 6 місяців тому +1

    UNDERSTOOD....Thank You So Much for this wonderful video............🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @ugempireninjaaaaaahatori6755
    @ugempireninjaaaaaahatori6755 9 місяців тому +1

    It is similar ques to the previous one , where we calculated the proviences, but the diff is here we are working with matrix

    • @AtharvaRao0104
      @AtharvaRao0104 2 місяці тому

      no they are not similar. Province problem was graph represented in a matrix. A grid is different from a matrix. In a grid each box is a node. where as in a matrix representation each row is associated with the node and tells which other nodes this node connects to..

  • @Mohit-mc6us
    @Mohit-mc6us 5 місяців тому

    00:08 Number of islands in a grid
    02:19 You can achieve complete coverage of the connected nodes by performing three traversals.
    04:34 Three islands exist
    06:56 We are using adjacency list to find neighbors
    09:18 BFS traversal starting points
    11:36 The code describes the implementation of a BFS traversal for a character grid
    13:38 Implement BFS algorithm to mark cells in a grid
    15:47 Learn a shortcut to find the neighbors of a given location on a grid
    17:59 You can easily get all the neighbors using the given technique.
    19:58 Use BFS algorithm with a matrix to solve a particular problem.
    22:00 The space complexity is approximately O(n^2) and the time complexity is approximately O(n^2)
    23:45 The time complexity is roughly n square.

  • @YashJoshi-lt4gd
    @YashJoshi-lt4gd Місяць тому

    He says" I am touched" :))) wonderful explanation..

  • @LeylaLi-v4w
    @LeylaLi-v4w Рік тому +1

    I love this video! It's very clear and easy to follow. Thank you!!

  • @cinime
    @cinime 2 роки тому +4

    Understood! Super great explanation as always, thank you very much!!

  • @googleit2490
    @googleit2490 Рік тому +1

    Graph Revision:
    Done and dusted in Grarph Revision (DFS)
    (9 mins)
    Nov'24, 2023 10:58 pm

  • @rohitshrirame2378
    @rohitshrirame2378 2 роки тому

    understood!!! learnt new trick for finding neighbours
    i first tried it in iterative way then wathed the video

  • @artofwrick
    @artofwrick 10 місяців тому

    We might store the movements in two individual arrays for row and column. And then we may do it.

  • @manishkumaryadav2395
    @manishkumaryadav2395 2 роки тому +3

    We can also do it without using another visited array by changing in the same array.
    Your explanation quality is so smooth and fantastic. Thank You.
    void f(int i,int j,vector&grid)
    {
    if(i=grid[0].size() || grid[i][j]=='0') return;
    grid[i][j]='0';
    for(int dx=-1;dx

    • @amanbhadani8840
      @amanbhadani8840 2 роки тому +1

      Doing manipulation in the original data is not preferred and not allowed in industry.

  • @Bhuwansp1
    @Bhuwansp1 2 роки тому +1

    understood 😀and wow i learn new techniques for visiting all neighbours

  • @kapilpatel9379
    @kapilpatel9379 Рік тому

    Question in leetcode and GFG has different neighbor conditions, thanks that I'm able to solve both

  • @bhanupratapsharma6158
    @bhanupratapsharma6158 2 роки тому +1

    Amazing your vedio question always on leectode and which helps to make profile amazing soo much thank you sir

  • @heartout9757
    @heartout9757 14 днів тому

    here instead of taking visited array we can directly update grid array to '0' to decrease space complexity

  • @shrutichouksey1324
    @shrutichouksey1324 4 місяці тому +1

    Thank you so much bhaiya for this series

  • @aditya_raj7827
    @aditya_raj7827 10 місяців тому +1

    Understood 😊

  • @udaypandey5659
    @udaypandey5659 10 місяців тому +1

    Understood 😎

  • @TheExtremeFizz
    @TheExtremeFizz Рік тому

    Leetcode code for cpp ->
    //the intution is just that we need to count the total number of disconnected components while doing bfs or dfs in the graph
    class Solution {
    public:
    void bfs(int r,int c,vector& grid,vector& vis)
    {
    int n=grid.size();
    int m=grid[0].size();
    vis[r][c]=1;
    queue q;
    q.push({r,c});

    while(!q.empty())
    {
    int row=q.front().first;
    int col=q.front().second;
    q.pop();

    int delRow[4]={-1,0,1,0};
    int delCol[4]={0,1,0,-1};

    for(int i=0;i=0 && nrow=0 && ncol

    • @kunalakhade5660
      @kunalakhade5660 11 місяців тому

      Why striver code isn't working on leetcode?? My code which is similar to striver ran successfully on gfg but not on leetcode??

  • @traveltalesDP
    @traveltalesDP 6 місяців тому

    Best explaination so far. Appreciate it!
    Thanks

  • @farazjawaid2982
    @farazjawaid2982 Рік тому +1

    was a challenging one, also very well explained!!

  • @jaishriharivishnu
    @jaishriharivishnu 5 місяців тому

    kya baat hai boss, ekdum faadu

  • @ianiketjain
    @ianiketjain Місяць тому

    Beautifully explained 🎉❤❤❤

  • @hj1752
    @hj1752 7 місяців тому

    Leetcode problem dont want to consider diagonal elements:
    so slight modify:
    for(int delrow = -1;delrow

  • @kartikrameshchavan6662
    @kartikrameshchavan6662 Рік тому +1

    Understood 🙏

  • @anvesh5377
    @anvesh5377 2 роки тому

    Understood 💙. Bhaiya one suggestion. Don't display the Java code for a long time.. Keep it for 5 -10 seconds.

    • @takeUforward
      @takeUforward  2 роки тому +6

      Java walon ko dikkat hga bro. C++ wle ko issues nai hota utna

  • @ramanpareek5218
    @ramanpareek5218 4 місяці тому

    Liked the video, notes taken, understood

  • @shreyxnsh.14
    @shreyxnsh.14 14 днів тому

    We dont even need visited array:
    class Solution {
    public:
    void bfs(int startI, int startJ, vector& grid) {
    int n = grid.size();
    int m = grid[0].size();
    queue q;

    q.push({startI, startJ});
    grid[startI][startJ] = '0'; // Mark as visited by changing to '0'

    // 4 directions: up, right, down, left
    int dr[] = {-1, 0, 1, 0};
    int dc[] = {0, 1, 0, -1};

    while (!q.empty()) {
    int row = q.front().first;
    int col = q.front().second;
    q.pop();

    // Check all 4 directions
    for (int i = 0; i < 4; i++) {
    int newRow = row + dr[i];
    int newCol = col + dc[i];

    if (newRow >= 0 && newRow < n && newCol >= 0 && newCol < m
    && grid[newRow][newCol] == '1') {
    grid[newRow][newCol] = '0'; // Mark as visited
    q.push({newRow, newCol});
    }
    }
    }
    }

    int numIslands(vector& grid) {
    if (grid.empty()) return 0;

    int islands = 0;
    int n = grid.size();
    int m = grid[0].size();

    for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
    if (grid[i][j] == '1') {
    islands++;
    bfs(i, j, grid);
    }
    }
    }

    return islands;
    }
    };

  • @tharunkumar8507
    @tharunkumar8507 Рік тому +1

    Understood. Thank you.

  • @rawat_ji5183
    @rawat_ji5183 2 роки тому +9

    I have done this dfs sol and feel very confident after the solution got submitted successfully thank you striver 😁😁
    void bfs(vector &value , vector&vis,int row,int col)
    {
    vis[row][col]=1;
    int r= value.size();
    int c= value[0].size();

    for(int i=-1;i

    • @amitranjan6998
      @amitranjan6998 2 роки тому +3

      you have written dfs code ,function is bfs ;)

    • @rawat_ji5183
      @rawat_ji5183 2 роки тому +2

      @@amitranjan6998 yaa my function name is bfs but code is dfs 😅😅😅😅

  • @abhinavkumar6584
    @abhinavkumar6584 2 роки тому +4

    btw understood and liked.Although please elaborate a little in depth on the TC and SC part atleast in the few videos.

    • @takeUforward
      @takeUforward  2 роки тому +4

      Since we are reading graphs, am assuming you are well versed with time complexity. If you are not, I think you should get those basics right before getting deep into graphs 😅

    • @abhinavkumar6584
      @abhinavkumar6584 2 роки тому

      @@takeUforward fair enough. Although I do have basic understanding but I always get lost for recursion cases. So does watching your first 7 videos from recursion playlist will do the trick for TC part?

    • @sonit9707
      @sonit9707 2 роки тому

      @@abhinavkumar6584 First 9 vidoes should be good.

    • @abhinavkumar6584
      @abhinavkumar6584 2 роки тому

      @@sonit9707 thanks..although I finished the playlist already...but thanks again for replying

  • @akhilappini3824
    @akhilappini3824 Рік тому +1

    abs(delrow - delcol) == 1 condition should be added

  • @Sillysmiles76
    @Sillysmiles76 Рік тому

    Thank you, Striver. Understood.

  • @UECAshutoshKumar
    @UECAshutoshKumar Рік тому +1

    Thank you sir 😊

  • @vishalgowrav
    @vishalgowrav Рік тому

    Understood!! Awesome Explanation...

  • @atharvadeshmukh6328
    @atharvadeshmukh6328 9 місяців тому

    leetcode - number of islands (not identical islands, that is a different question)
    // explore the vertex's 4 neighbours
    for(int drow = -1; drow = 0 and ncol < grid[0].size())
    and (grid[nrow][ncol] == '1')
    and (!vis[nrow][ncol]))
    {
    vis[nrow][ncol] = true;
    q.push({nrow, ncol});
    }
    }
    }
    }

  • @VikasBagri-i5j
    @VikasBagri-i5j 4 місяці тому

    understood.. thanks for this amazing series :)

  • @chitranshjain5075
    @chitranshjain5075 2 роки тому

    Completely UNDERSTOOD

  • @sripriyapotnuru5839
    @sripriyapotnuru5839 2 роки тому

    Thank you, Striver 🙂

  • @DevashishJose
    @DevashishJose 10 місяців тому

    Understood. Thank you so much.

  • @KUMARSAURABH-s5i
    @KUMARSAURABH-s5i 6 місяців тому

    Understood Striver!!

  • @arpitjaswal4210
    @arpitjaswal4210 7 місяців тому

    leetcode version, where we only have to check in 4 directions: class Solution {
    private:
    void bfs(vector& grid, vector& visited, int r, int c){
    int n = grid.size();
    int m = grid[0].size();
    visited[r][c]=1;
    queueq;
    q.push({r,c});
    while(!q.empty()){
    int r=q.front().first;
    int c=q.front().second;
    q.pop();
    for(int rdelta=-1;rdelta

  • @udaypratapsingh8923
    @udaypratapsingh8923 2 роки тому

    here we go!

  • @YATHARTHBHARDWAJ-y8m
    @YATHARTHBHARDWAJ-y8m Рік тому

    superb explanation

  • @umeshkaushik710
    @umeshkaushik710 2 роки тому +1

    Please also make articles for these questions on the website.

    • @takeUforward
      @takeUforward  2 роки тому

      In description there is a link

    • @umeshkaushik710
      @umeshkaushik710 2 роки тому +1

      @@takeUforward Yes, But that link in the description is for BFS traversal, not specifically for the "Number of islands" question.

  • @KunalSingh-kn2ij
    @KunalSingh-kn2ij 2 роки тому +1

    perfect explanation👍👍

  • @adebisisheriff159
    @adebisisheriff159 10 місяців тому

    Thanks so much Striver!!!!

  • @hrushi_borhade
    @hrushi_borhade Рік тому

    Understood Striver!

  • @abhishekdash4933
    @abhishekdash4933 Рік тому +2

    Hey @Striver, explanation is on point, realy helpful. One thing I noticed you explain the bfs storage to be a queue but u always represent on diagram a stack,
    As per my understanding in bfs we use queue FIFO and in DFS its stack LIFO. Feel free to correct if am wrong.

    • @SwatejTech
      @SwatejTech Рік тому +4

      he only draws it like a stack but he removes from the bottom only, just like a queue

  • @ankitranjan88
    @ankitranjan88 Рік тому +1

    solved DFS of it's by OWN

  • @abhiyadav8500
    @abhiyadav8500 Рік тому

    No one can explain this....as easy as you do....🥹

  • @shivamsoni5602
    @shivamsoni5602 2 роки тому

    thanks brother for explain this question in very easy way thank you so much

  • @manishkumarsah6898
    @manishkumarsah6898 2 роки тому +1

    Thank you so much, it was superb

  • @vikasbagri1225
    @vikasbagri1225 2 роки тому

    understood it very well...

  • @DhruvToshniwal
    @DhruvToshniwal 2 роки тому +4

    Why does this algorithm need visited array? Without visited array also it should work. Because a value of 0 in grid essentially means that it is either water or already visited
    Just add grid[newX][newY] = '0' at line 27 and grid[row][col] = '0' at line 9 and space complexity needed will O(1). No need for vis array

    • @brothin3007
      @brothin3007 2 роки тому +3

      He said in the later part of the video that without even using an additional grid, we can use the same grid to mark islands as visited (replacing 1s with 0s) but yeah, considering the given grid to be part of space or not depends on us.

    • @sumerrawat6947
      @sumerrawat6947 2 роки тому +1

      nice !

    • @valobhediya
      @valobhediya 2 роки тому

      Can you share your code?

    • @takeUforward
      @takeUforward  2 роки тому +7

      Space complexity is never O(1) when you use some thing to modify, the definition of space complexity is the space used in an algorithm, and is not what you recreated, you need to understand this.

    • @valobhediya
      @valobhediya 2 роки тому

      @@takeUforward Sir, is that auxiliary Space Complexity you're talking about!

  • @AppaniMadhavi
    @AppaniMadhavi 6 місяців тому

    Thank you so much striver

  • @TravelTracksByDebo
    @TravelTracksByDebo 2 роки тому

    Awesome explanation ... understood

  • @harshvardhansingh2272
    @harshvardhansingh2272 2 роки тому

    understood striver
    great explanation👍

  • @vijayarana2087
    @vijayarana2087 Рік тому

    Thankyou soo much for deep explanation

  • @piyushacharya7696
    @piyushacharya7696 2 роки тому

    Loved your explanation😍😍😍😍

  • @sidduroy9150
    @sidduroy9150 2 роки тому

    As always you are a rock star ✨

  • @ganavin3423
    @ganavin3423 2 роки тому

    understood bhaiya

  • @selvaganeshm1914
    @selvaganeshm1914 Рік тому

    Best series

  • @morganyu3391
    @morganyu3391 2 роки тому

    Understood bhaiya

  • @anikethdeshpande8336
    @anikethdeshpande8336 Рік тому

    best explanation !

  • @SatyamKumar-bw4vi
    @SatyamKumar-bw4vi Рік тому

    Jay Jagannath Striver..!
    I think we don't have to go all 8 direction only 4 direction is enough but very good video

  • @hashcodez757
    @hashcodez757 Рік тому

    understood!! SIR