Number of Islands | LeetCode 200 | Google Coding Interview Tutorial
Vložit
- čas přidán 26. 07. 2024
- Number of Islands solution: LeetCode 200
Code and written explanation: terriblewhiteboard.com/number...
Link to problem: leetcode.com/problems/number-...
Buy Me a Coffee: www.buymeacoffee.com/terrible...
AFFILIATE LINKS
If you're interested in learning algorithms, these are great resources.
💻 40% off Tech Interview Pro: techinterviewpro.com/terriblew...
⌨️ 20% off CoderPro: coderpro.com/terriblewhiteboard
💲 All coupons and discounts 💲
terriblewhiteboard.com/coupon...
Number of Islands | LeetCode 200 | Google Coding Interview
#numberofislands #leetcode #algorithms #terriblewhiteboard #codinginterview
Click the time stamp to jump to different parts of the video.
00:00 Title
00:06 Problem readout
00:58 Whiteboard solution
07:42 Coding solution
17:55 Result and outro
If there are any videos you'd like me to make or if you have any ideas on how to optimize this solution, let me know!
994. Rotting Oranges
the best explanation I’ve ever seen so far. Great work! Thank you
Thank you for the tutorial! You're a great teacher.
Thank you. You just saved my life with this detailed video!
Great video can you please also review the time and memory complexity
I love that you draw graphs in your videos to demonstrate. It makes it a lot easier to understand than just showing the code.
Thanks!
Wonderful explanation that I have ever listened to for 2d Array-based problem!
Thanks!
beautiful awsome amazing explanation i love you I'm from a non-English speaking country and i'm not good at English but i can understand this
fantastic explanation
I am glad I found a very good explanation in JS. Thank you so much! Please keep making more!
Thanks!
Keep it up. Your explanations are simple and awesome!
Thanks!
I have personally struggled with implementing BFS and DFS into code for more than half a year now, asking others for help, searching online for explanations, solutions, etc. and nothing helped me make any progress at all. Your explanation of this taught me how to do it in 20 minutes. Thank you so so much dude. You've helped me so much more than anyone else. Even professors in my university.
Awesome. Glad to hear!
Amazing Explanation! I really understood the problem very well as well as the solution. Even though I code in Python as I understood your logic I was able to convert it from JS to Python and run it successfully! Thank You soo much for making a video explaining the problem and the solution along with the code. I will make sure to see all of your other videos and understand the logic and convert them to code in Python! Thanks a lot for taking out the time to make a video explaining everything. Helped me save a lot of time and understanding everything quickly within half an hour!
Glad to help!
Thanks mate
Thank you!
Please make more videos. I have asked all my friends to subscribe to your channel
Soon.
Awesome Video! I wonder why have we stopped creating content? ?is it because of time commitments? By the way, i loved the music at the start as well! Thanks again for this!
When I solved this instead of using "i" and "j" for the nested for loop iterating over every element. I called them "row" and "column" respectively to make it a bit easier to understand.
Yeah, that would have helped.
Nice vid, you might also wanna include the space and time complexities
Thanks!
Is the time complexity O(m*n) (row x column), and space is O(n)? I am not sure about space complexity
@@datduong6520 Why do you think space complexity is O(n) In my opinion it is just the space for storing the numofIslands which is an integer since we are modifying the matrix to keep track of visited
Once again THANK YOU!!
Following the same logic in Java :)
class Solution {
int row;
int column;
public int numIslands(char[][] grid) {
if(grid == null || grid.length == 0){
return 0;
}
int countIsland = 0;
row = grid.length;
column = grid[0].length;
for(int r = 0; r < row; r++){
for(int c = 0; c < column; c++){
if (grid[r][c] == '1') {
getNumberOfIslands(grid, r, c);
countIsland++;
}
}
}
return countIsland;
}
private void getNumberOfIslands(char[][] grid, int r, int c) {
if (r < 0 || c < 0 || r >= row || c >= column || grid[r][c] != '1'){
return;
}
grid[r][c] = '0';
getNumberOfIslands(grid, r + 1, c);
getNumberOfIslands(grid, r - 1, c);
getNumberOfIslands(grid, r, c + 1);
getNumberOfIslands(grid, r, c - 1);
}
}
Hi, Could you please include time and space complexities?
Good video. One nit pick around 8:45 you called it 2 arrays and I think you meant to say 2d array.
Yup! You're right.
The explanation is fantastic. But it would be even better if it explained better the recursive function, even though I understand it is not the main point of this problem.
Loved the explanation, I wanted to ask a question though. The current TC is O(Elements). What happens in case we don't turn the island 0 and let it be 1. I know it will result in a bad time complexity because in worst case it will be O(Element^4) but as I am getting another error than TLE that is why I wanted to ask just in case to clarify what possibilities of Runtime error are present.
What is the time complexity of this algorithym?
Hmmmm