Number of Closed Islands - Leetcode 1254 - Python

preview_player
Показать описание

Solving leetcode 1254 - Number of Closed Islands, today's daily leetcode problem on April 5th.

0:00 - Read the problem
0:30 - Drawing Explanation
7:00 - Coding Explanation

leetcode 1254

#neetcode #leetcode #python
Рекомендации по теме
Комментарии
Автор

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.

ShikaIE
Автор

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.

schrodingerskatt
Автор

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.

Andrew-ddvf
Автор

Awesome explaination as always NEET yor are GOAT of LeetCode

JameS
Автор

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.

ameyakhot.official
Автор

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?

alexsinx
Автор

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?

vaibhavjade
Автор

This solution was so perfectly explained. Thank you.

MP-nyep
Автор

I like this person, because he is not a human being 😅😅😅HE REALLY FROM ALIENS 👽👽👽 WORLD 😅😅😅😊😊😊😊(REASON: IF I WATCH SAME PROBLEM IN OTHERS YOUTUBE 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??

vishnuvardhan
Автор

Couldn’t you also have your for loops start from, 1, ROWS -1 so you don’t check the borders?

goshua
Автор

i just watched 5 mins and coded by myself .. and I did it :)

MayankLC
Автор

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.

vixguy
Автор

Great work on the videos and explanation!!

akarsan
Автор

Are cpp codes also available? If it s, can you share the same.

haard_
Автор

For the Dfs could you also use booleans and just return Dfs in all directions using and?

julianelmasry
Автор

Why are we returning true for 1 which is not on the boundary?

ssss-mvdx
Автор

can someone pls explain why do we return true if we have already visited (r, c) in the base case?

gnidnert
Автор

can someone explain what are we doing here: res += dfs(r, c)

harshitpant
Автор

importance of (r, c) in visited in base case!!?

shubhangkhandelwal
Автор

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?

midorukawa