Number of Closed Islands - Leetcode 1254 - Python

  Рет қаралды 16,927

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 42
@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 11 ай бұрын
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.
@ameyakhot4458
@ameyakhot4458 10 ай бұрын
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 10 ай бұрын
You could have an island starting at row idx 1 but stretches all the way to boundary.
@Andrew-dd2vf
@Andrew-dd2vf 9 ай бұрын
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.
@vaibhavjade1545
@vaibhavjade1545 Ай бұрын
what about the case when there's water inside the land? we're exiting in that case and will run remaining set of land points, resulting in 2 counts for same island?
@alexsinx
@alexsinx Жыл бұрын
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?
@MP-ny3ep
@MP-ny3ep Жыл бұрын
This solution was so perfectly explained. Thank you.
@JameS00989
@JameS00989 Жыл бұрын
Awesome explaination as always NEET yor are GOAT of LeetCode
@MayankLC93
@MayankLC93 Жыл бұрын
i just watched 5 mins and coded by myself .. and I did it :)
@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??
@akarsan9121
@akarsan9121 Жыл бұрын
Great work on the videos and explanation!!
@haard_
@haard_ Жыл бұрын
Are cpp codes also available? If it s, can you share the same.
@mohithadiyal6083
@mohithadiyal6083 Жыл бұрын
Great explanation
@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 Жыл бұрын
@@atishayjain3011 Thanks for the explanation and saving my time !!
@skh551
@skh551 Жыл бұрын
Had the same issue. Thanks for mentioning@@atishayjain3011
@rixu353
@rixu353 11 ай бұрын
samer here, I think if you execute False & argment_1, argument_1 won't get executed
@ashish7516
@ashish7516 9 ай бұрын
@@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
@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!!?
@ssss-mv6dx
@ssss-mv6dx Ай бұрын
Why are we returning true for 1 which is not on the boundary?
@akhiladevangamath1277
@akhiladevangamath1277 20 күн бұрын
this means, if we have reached water on all 4 direction, that was a closed island, so we are counting it by 1
@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
@harshitpant07
@harshitpant07 Жыл бұрын
can someone explain what are we doing here: res += dfs(r,c)
@lonen3rd
@lonen3rd Жыл бұрын
6 months late reply, He explains it very well at 08:29
@amanrai5285
@amanrai5285 Ай бұрын
We do that to calculate the number of loops. If dfs return 0 then no closed loops whereas if it returns 1 then we add to the result.
@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.
@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
@deanliu7125
@deanliu7125 Жыл бұрын
Great explanation
Number of Enclaves - Leetcode 1020 - Python
11:39
NeetCodeIO
Рет қаралды 9 М.
Google Coding Interview Question - Number of Closed Islands (LeetCode)
21:03
風船をキャッチしろ!🎈 Balloon catch Challenges
00:57
はじめしゃちょー(hajime)
Рет қаралды 93 МЛН
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 10 М.
NUMBER OF ISLANDS - Leetcode 200 - Python
11:41
NeetCode
Рет қаралды 332 М.
Become a Malloc() Pro
6:58
thedoubleeguy
Рет қаралды 2,5 М.
5 Simple Steps for Solving Any Recursive Problem
21:03
Reducible
Рет қаралды 1,2 МЛН
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 197 М.
Number of Islands - LeetCode 200 Python
12:15
DEEPTI TALESRA
Рет қаралды 12 М.
How to Solve ANY LeetCode Problem (Step-by-Step)
12:37
Codebagel
Рет қаралды 300 М.
LeetCode was HARD until I Learned these 15 Patterns
13:00
Ashish Pratap Singh
Рет қаралды 577 М.
Detonate the Maximum Bombs - Leetcode 2101 - Python
11:20
NeetCodeIO
Рет қаралды 14 М.
Construct Quad Tree - Leetcode 427 - Python
12:26
NeetCodeIO
Рет қаралды 25 М.