Week 9
Today I looked at some interesting videos of some leetcode videos, which use problems that show up at coding interviews. I watched a guy who was prepping for interviews by running through these problems and talking about them. First Video was about locating islands. (pic of the instructions below)
So hopefully you can see how the first input has only one island because all the ones are surrounded by zeros. It's kinda hard to see at first but it helped when he points out why in the video. He talks about the problem and comes up with a pretty straightforward solution. He says he will use a breadth first search(BFS), which I've done a bit of research on before in my week 6 of last years blog. Breadth first search when used on binary search trees takes a node and searches all of its nodes, so I can see how it can apply to this problem. Breadth first search will take one island(1) and search all the 1s connected, which is an island, so when it can't find anymore it means there are no more islands or there are no more to the current island. He says while coding the solutions that he will first use a double for loop to search through x and y of the input. In the for loops it will search for a one and add to a count and call the BFS method and at the very end return the count, which is the number of islands. The BFS method will take in the grid, int i and int j. First thing in the method is he creates a boundary check where he checks if i is less then zero, i is greater than or equal to the length of the grid , j is less than zero, j is greater than grid i length and grid i j is zero (so it will terminate if there is a mistake and passes in a zero). So how the actual method will work is pass in a one and make each one a zero tell there are no one around and no one left. (illustration of my understanding)
01100
01100
00001
00100
01100
00000
00000
01100
00000
00000
00100
00000
00000
00000
00001
00000
00000
00000
done
the code that I tried to illustrate. Very interesting and very cool that I got to see a use for something I lightly went over a bit ago


Comments
Post a Comment