No video

Number of Closed Islands - Leetcode 1254 - Python

  Рет қаралды 15,035

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 38
@mrmcpherson2722
@mrmcpherson2722 Жыл бұрын
love the daily leetcode problem vids
@ShikaIE
@ShikaIE Жыл бұрын
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
@Tim_desu Жыл бұрын
such a subtle bug, thx dude!
@janhavipatil4971
@janhavipatil4971 Жыл бұрын
Thank you for pointing this out!
@patchouli9
@patchouli9 Жыл бұрын
0:46 Amogus
@schrodingerskatt222
@schrodingerskatt222 8 ай бұрын
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-dd2vf
@Andrew-dd2vf 6 ай бұрын
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.
@ameyakhot4458
@ameyakhot4458 8 ай бұрын
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.
@prashanthkurella4500
@prashanthkurella4500 7 ай бұрын
You could have an island starting at row idx 1 but stretches all the way to boundary.
@vishnuvardhan2687
@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??
@alexsinx
@alexsinx 9 ай бұрын
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
@JameS00989 Жыл бұрын
Awesome explaination as always NEET yor are GOAT of LeetCode
@vixguy
@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
@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).
@vaishnavinatarajan3987
@vaishnavinatarajan3987 11 ай бұрын
@@atishayjain3011 Thanks for the explanation and saving my time !!
@skh551
@skh551 10 ай бұрын
Had the same issue. Thanks for mentioning@@atishayjain3011
@rixu353
@rixu353 8 ай бұрын
samer here, I think if you execute False & argment_1, argument_1 won't get executed
@ashish7516
@ashish7516 6 ай бұрын
@@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
@MP-ny3ep Жыл бұрын
This solution was so perfectly explained. Thank you.
@MayankLC93
@MayankLC93 Жыл бұрын
i just watched 5 mins and coded by myself .. and I did it :)
@deanliu7125
@deanliu7125 Жыл бұрын
Great explanation
@akarsan9121
@akarsan9121 Жыл бұрын
Great work on the videos and explanation!!
@julianelmasry9556
@julianelmasry9556 Жыл бұрын
For the Dfs could you also use booleans and just return Dfs in all directions using and?
@akarsan9121
@akarsan9121 Жыл бұрын
Yes, that can be done too
@shubhangkhandelwal9113
@shubhangkhandelwal9113 Жыл бұрын
importance of (r,c) in visited in base case!!?
@haard_
@haard_ Жыл бұрын
Are cpp codes also available? If it s, can you share the same.
@midorukawa3459
@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
@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
@harshitpant07 Жыл бұрын
can someone explain what are we doing here: res += dfs(r,c)
@lonen3rd
@lonen3rd 10 ай бұрын
6 months late reply, He explains it very well at 08:29
@gnidnert2833
@gnidnert2833 Жыл бұрын
can someone pls explain why do we return true if we have already visited (r,c) in the base case?
@tonyz2203
@tonyz2203 Жыл бұрын
same question
@julianelmasry9556
@julianelmasry9556 Жыл бұрын
Why can we return true if we have seen it before can anyone explain
@treyawey3878
@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
@pranav7478 Жыл бұрын
@SandeepKumar-vk3iq
@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
@roywastaken
@roywastaken Жыл бұрын
maestro
@mohithadiyal6083
@mohithadiyal6083 Жыл бұрын
Great explanation
Number of Enclaves - Leetcode 1020 - Python
11:39
NeetCodeIO
Рет қаралды 8 М.
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 5 М.
КТО ЛЮБИТ ГРИБЫ?? #shorts
00:24
Паша Осадчий
Рет қаралды 4,3 МЛН
АЗАРТНИК 4 |СЕЗОН 1 Серия
40:47
Inter Production
Рет қаралды 1,1 МЛН
Coin Change - Dynamic Programming Bottom Up - Leetcode 322
19:23
Dota2 Senate - Leetcode 649 - Python
14:48
NeetCodeIO
Рет қаралды 20 М.
Python Looping Mastery Beyond the Basics🚀
13:32
The Data Engineering Channel
Рет қаралды 55
Medium Google Coding Interview With Ben Awad
51:27
Clément Mihailescu
Рет қаралды 1,3 МЛН
NUMBER OF ISLANDS - Leetcode 200 - Python
11:41
NeetCode
Рет қаралды 299 М.
Google Coding Interview Question - Number of Closed Islands (LeetCode)
21:03
NUMBER OF PROVINCES (Leetcode) - Code & Whiteboard
13:05
babybear4812
Рет қаралды 19 М.
Graph Algorithms for Technical Interviews - Full Course
2:12:19
freeCodeCamp.org
Рет қаралды 1,2 МЛН
КТО ЛЮБИТ ГРИБЫ?? #shorts
00:24
Паша Осадчий
Рет қаралды 4,3 МЛН