Just fyi for others, be careful when visiting neighbors if your dfs return boolean instead of count like here. E.g: boolean dfs(grid, r, c) { …code here.. return dfs(grid, r+1, c) && dfs(grid, r-1, c) && ..the rest here..; } Doing this way will make you stop early and only half the island is visited. When the main for loop reach the unvisited part of the island again, the result will be wrong.
@Tim_desu Жыл бұрын
such a subtle bug, thx dude!
@janhavipatil4971 Жыл бұрын
Thank you for pointing this out!
@patchouli9 Жыл бұрын
0:46 Amogus
@schrodingerskatt2228 ай бұрын
If I'm able to make it to MAANG or any big tech, it would be only because of you Navi. Thankyou so much, I learn a lot from your channel.
@Andrew-dd2vf6 ай бұрын
You can alternatively use the bitwise AND operator (&) to combine the dfs statements, assuming it returns False when OOB and True otherwise. Reason: if using "and", once you encounter the first dfs statement and it's false, the remaining dfs statements won't be executed (short-circuit), which will misrepresent the visited grids in future iterations of grid.
@ameyakhot44588 ай бұрын
We can also run the loop from 1 to row_len - 1 and 1 to col_len - 1. Because the land cells in the boundary are never going to be a part of the enclosed islands since minimum one side is boundary.
@prashanthkurella45007 ай бұрын
You could have an island starting at row idx 1 but stretches all the way to boundary.
@vishnuvardhan2687 Жыл бұрын
I like this person, because he is not a human being 😅😅😅HE REALLY FROM ALIENS 👽👽👽 WORLD 😅😅😅😊😊😊😊(REASON: IF I WATCH SAME PROBLEM IN OTHERS KZbin CHANNES LIKE 10000 TIMES I CANT ABLE TO UNDERSTAND,SO ALIENS ONLY CREATE WONDERS).SIR PLEASE SUGGEST ME HOW TO BECOME LIKE YOU??? DEFINITELY HARDWORK MATTERS,OTHER THAN THESE??
@alexsinx9 ай бұрын
I just don't get why the time complexity is only O(n*m) once you have two nested for loops and additional recursive calls inside of them, wouldn't you have to consider the time complexity of the recursive calls either?
@JameS00989 Жыл бұрын
Awesome explaination as always NEET yor are GOAT of LeetCode
@vixguy Жыл бұрын
Hi NeetCode! I tried the same thing but I used true and false instead of 0s and 1s. I have no idea why this turns out to not work for a few testcases.
@atishayjain3011 Жыл бұрын
I just found the reason for this. It's because in the case where you're ANDing all of these traversals, python will stop if it reaches a false condition, whereas you want to go forward with the entire traversal no matter what. The way to do it is to have each statement execute (perhaps set it equal to four variables and then to return the and of those four variables).
@vaishnavinatarajan398711 ай бұрын
@@atishayjain3011 Thanks for the explanation and saving my time !!
@skh55110 ай бұрын
Had the same issue. Thanks for mentioning@@atishayjain3011
@rixu3538 ай бұрын
samer here, I think if you execute False & argment_1, argument_1 won't get executed
@ashish75166 ай бұрын
@@atishayjain3011And why do we want to visit each cell. If we don’t visit some branch in this iteration, it should remain unvisited and we should cover it when we reach this cell from outer for loop. We would still iterate each cell once only. So, why is this an issue
@MP-ny3ep Жыл бұрын
This solution was so perfectly explained. Thank you.
@MayankLC93 Жыл бұрын
i just watched 5 mins and coded by myself .. and I did it :)
@deanliu7125 Жыл бұрын
Great explanation
@akarsan9121 Жыл бұрын
Great work on the videos and explanation!!
@julianelmasry9556 Жыл бұрын
For the Dfs could you also use booleans and just return Dfs in all directions using and?
@akarsan9121 Жыл бұрын
Yes, that can be done too
@shubhangkhandelwal9113 Жыл бұрын
importance of (r,c) in visited in base case!!?
@haard_ Жыл бұрын
Are cpp codes also available? If it s, can you share the same.
@midorukawa3459 Жыл бұрын
Thank you so much for this video, very helpful! :) Just out of curiosity: is there a reason why ROWS and COLS are written in capitals? Is it a convention or just a personal choice?
@treyawey3878 Жыл бұрын
It's sort of a convention to write constants in all caps. Makes it easier to identify them. Our company codebase follows this practice at least.
@harshitpant07 Жыл бұрын
can someone explain what are we doing here: res += dfs(r,c)
@lonen3rd10 ай бұрын
6 months late reply, He explains it very well at 08:29
@gnidnert2833 Жыл бұрын
can someone pls explain why do we return true if we have already visited (r,c) in the base case?
@tonyz2203 Жыл бұрын
same question
@julianelmasry9556 Жыл бұрын
Why can we return true if we have seen it before can anyone explain
@treyawey3878 Жыл бұрын
Because we're coming back to a piece of land on a grid we've already visited, that means we haven't encountered land on the edge of the grid along that path.
@pranav7478 Жыл бұрын
❤
@SandeepKumar-vk3iq Жыл бұрын
Hey bro great content i love your channel however i used this solution and it failed so i am curious why it failed as logic is correct @neetcode