Thank you for this video! I couldn’t understand LeetCode's or any other explanation of LeetCode 694 (another island problem). Then, TH-cam brought me here. Your explanation of LeetCode 1254 made me understand LeetCode 694!! LeetCode should hire you because you’re so talented at explaining solutions!!!
One suggestion. In a recursive function, explain the recursive approach first then the base condition so that we can understand why exactly the base condition was written. Thanks for the video :)
I believe the space complexity would be O(max(m,n)) and not O(m*n). This is because at the worst limit, we are going to have the recursion call stack depth as max(m,n).
How are we checking that our island is indeed surrounded by 1's. In the above example what if the top bottom rows and left right columns were all zeroes
why do you need to store left, right, up, down into varaibles instead of directly returning all of those dfs statements with && together. i know this doesn't work but haven't been able to figure out the reason. appreciate any help thank you
Hi @Michael Muinos Excellent Solution. But I was having one doubt regarding the All 4 direction recursive call. Why does the below recursion call returns the wrong Answers? I think it should work similar to your left , right, up & down // code return isClosedIsland(grid,i,j-1) && isClosedIsland(grid,i,j+1) && isClosedIsland(grid,i-1,j) && isClosedIsland(grid,i+1,j);
because when you are calling the function with and operator, if any function call returns false, the rest are not going to execute, and as a result all the results are not going to marked as -1.so again when you revisit the unmarked zero, it will return true, resulting in more no of closed island.
I was struggling to solve this problem, but with your hint for the problem : 2:20, I was able to solve the problem completely. class Solution { public int closedIsland(int[][] grid) { int m = grid.length, n=grid[0].length; for(int i=0; i
Sharing my solution without using -1 for tracking visits. class Solution: def closedIsland(self, grid: List[List[int]]) -> int: m = len(grid) n = len(grid[0]) def isClosedIsland(i, j): if i < 0 or j = m or j >= n: return False // if it reaches here, that means land is touching to the edge if grid[i][j] == 1: return True // from this direction, water is covering before the land reaches the edge grid[i][j] = 1 // turning land into water to indicate visit left = isClosedIsland(i, j-1) right = isClosedIsland(i, j+1) up = isClosedIsland(i-1, j) down = isClosedIsland(i+1, j) return left and right and up and down
count = 0 for i in range(m): for j in range(n): if grid[i][j] == 0 and isClosedIsland(i, j ): count +=1
Thank you for this video! I couldn’t understand LeetCode's or any other explanation of LeetCode 694 (another island problem). Then, TH-cam brought me here. Your explanation of LeetCode 1254 made me understand LeetCode 694!! LeetCode should hire you because you’re so talented at explaining solutions!!!
Thank you so much!
One suggestion.
In a recursive function, explain the recursive approach first then the base condition so that we can understand why exactly the base condition was written. Thanks for the video :)
very elegant. This is a solution you can realistically come up with during a live interview than some fancy solutions on leetcode.
Exactly, I like this approach alot
you always have the better explanations, compared to other channels. Thanks a lot and please keep doing these amazing videos
I really appreciate that, that is my main goal!
Your channel is so underrated!
Thank you!!
Thanks a lot for such a great and in-depth explanation of the problem. Really appreciate the way you explained the complex problem in simpler manner!
Glad you liked it!
Great explanation! So much clearer than any of the explanations on LeetCode. Thank you for the awesome video!
Awesome, that is the goal! Thanks for watching
the clearest explanation i found on this problem! awesome content Michael!
Great to hear!
Thanks for the clear explanation. Keep up the great work, really appreciate it!
Will do, thanks for watching!
I believe the space complexity would be O(max(m,n)) and not O(m*n). This is because at the worst limit, we are going to have the recursion call stack depth as max(m,n).
So clear. Thank you very much
Thanks mate, you made it really easy to understand
No problem 👍
AMAZING MAN, i hope i do well in my interview
Thanks, best of luck!
You are awesome man! Loved your way of explanation.
Thanks a ton!
Nice explanation! Carry on. Waiting for more to come!! Cheers!
Thanks, will do!
Thanks man! After the full length example, I was able to solve it :D
Nice! Example walkthroughs are the key to understanding these problems
Indeed. I just hope that by upsolving these questions I get to tackle similar problems by myself!
The more you do it, the easier it will be. simple as that!
Awesome Explanation dude
Thanks man!
Thank you so much for this video! Very helpful!
You are so welcome!
Good Explanation! Thanks :)
No problem!
How are we checking that our island is indeed surrounded by 1's. In the above example what if the top bottom rows and left right columns were all zeroes
Yes, He compleely ignored this possibility
i think it's the base condition when he's checking if the cell is 1, then return true
Can you please make a video of this Q solving with Union-Find method?
why do you need to store left, right, up, down into varaibles instead of directly returning all of those dfs statements with && together. i know this doesn't work but haven't been able to figure out the reason. appreciate any help thank you
Another great explanation by you sir.....
Thanks
Thank you for the kind words!
Hi @Michael Muinos Excellent Solution. But I was having one doubt regarding the All 4 direction recursive call.
Why does the below recursion call returns the wrong Answers? I think it should work similar to your left , right, up & down
// code
return isClosedIsland(grid,i,j-1) && isClosedIsland(grid,i,j+1) && isClosedIsland(grid,i-1,j) && isClosedIsland(grid,i+1,j);
because when you are calling the function with and operator, if any function call returns false, the rest are not going to execute, and as a result all the results are not going to marked as -1.so again when you revisit the unmarked zero, it will return true, resulting in more no of closed island.
@@nishantduttmishra9290 Thank-you for answering the question! this helped me a lot
Excellent man! You are so cool!
Haha thanks man, appreciate it!
smooth AF
great explanation ......
you are pretty smart!
I was struggling to solve this problem, but with your hint for the problem : 2:20, I was able to solve the problem completely.
class Solution {
public int closedIsland(int[][] grid) {
int m = grid.length, n=grid[0].length;
for(int i=0; i
Many months late on the reply, but nice job!
Thank you so much as always!
You are so welcome!
good explanation! you write neat code : )
I try! Thank you so much
What if there is a 0 on the perimeter that makes the island not closed???!!?!
0:05 - I think I got a theory why six figure tech FANGG employees asking island questions, they probably miss their recent Hawaii trips... lol 😂
brilliant
Will it work for this case? grid=[[0,0,1,0,0], [0, 0, 0, 1, 0], [0, 1,1,1,0]]
I'm not sure, I would test it on LeetCode
Can we change that 0 to 1 instead of -1 ? I guess it work as well ?
Yea, I think it would work the same.
Great
Thank you!
Tc- (m*n) , Sc - (m*n)
time complexity isn't m*n i feel coz there are 4 recursive calls so it would be 4^(mn) i believe
Each node gets visited only once
Sharing my solution without using -1 for tracking visits.
class Solution:
def closedIsland(self, grid: List[List[int]]) -> int:
m = len(grid)
n = len(grid[0])
def isClosedIsland(i, j):
if i < 0 or j = m or j >= n:
return False // if it reaches here, that means land is touching to the edge
if grid[i][j] == 1:
return True // from this direction, water is covering before the land reaches the edge
grid[i][j] = 1 // turning land into water to indicate visit
left = isClosedIsland(i, j-1)
right = isClosedIsland(i, j+1)
up = isClosedIsland(i-1, j)
down = isClosedIsland(i+1, j)
return left and right and up and down
count = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 0 and isClosedIsland(i, j ):
count +=1
return count