Google Coding Interview Question - Number of Closed Islands (LeetCode)
Here is a step by step explanation of another "island" problem asked at Google.
🎧 Join the community Discord: https://discord.gg/aVWsAaaCtT
💰 Support me on Patreon: https://www.patreon.com/michaelmuinos
🔗Follow me on LinkedIn: https://www.linkedin.com/in/michael-muinos
📂Follow me on Github: https://github.com/MichaelMuinos
We have another addition to the "island" problem family. This problem is asked at Google and can be solved using a Breadth-first search or Depth-first search. For this tutorial, I go over the DFS solution because it is a bit easier to understand in comparison to the BFS solution.
To solve this problem, we must identify what constitutes a "closed" island. A closed island is a group of 0's that are entirely surrounded by water (1's). The trick to solving this problem is identifying that any island that touches the perimeter of our matrix will for sure NOT be a closed island. The reason this is the case is because if our island is on the perimeter, it is impossible for it to be surrounded since it is at the maximum bounds already.
With this information, we do not need to iterate over our perimeter, only the elements within the center of our matrix. As we are iterating, when we encounter a 0, we perform our DFS starting at that position and change any 0's we see to -1 to illustrate that we have visited that position. An alternative to this approach is to have a 2D boolean array instead of modifying our input; however, modifying our input is easier to write and involves less code!
Finally, when we are converting our 0's to -1's, if our position is on the perimeter and it is a 0, we know it is not a closed island and we can return false from our function that is determining if it is a closed island or not.
Видео Google Coding Interview Question - Number of Closed Islands (LeetCode) канала Michael Muinos
🎧 Join the community Discord: https://discord.gg/aVWsAaaCtT
💰 Support me on Patreon: https://www.patreon.com/michaelmuinos
🔗Follow me on LinkedIn: https://www.linkedin.com/in/michael-muinos
📂Follow me on Github: https://github.com/MichaelMuinos
We have another addition to the "island" problem family. This problem is asked at Google and can be solved using a Breadth-first search or Depth-first search. For this tutorial, I go over the DFS solution because it is a bit easier to understand in comparison to the BFS solution.
To solve this problem, we must identify what constitutes a "closed" island. A closed island is a group of 0's that are entirely surrounded by water (1's). The trick to solving this problem is identifying that any island that touches the perimeter of our matrix will for sure NOT be a closed island. The reason this is the case is because if our island is on the perimeter, it is impossible for it to be surrounded since it is at the maximum bounds already.
With this information, we do not need to iterate over our perimeter, only the elements within the center of our matrix. As we are iterating, when we encounter a 0, we perform our DFS starting at that position and change any 0's we see to -1 to illustrate that we have visited that position. An alternative to this approach is to have a 2D boolean array instead of modifying our input; however, modifying our input is easier to write and involves less code!
Finally, when we are converting our 0's to -1's, if our position is on the perimeter and it is a 0, we know it is not a closed island and we can return false from our function that is determining if it is a closed island or not.
Видео Google Coding Interview Question - Number of Closed Islands (LeetCode) канала Michael Muinos
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Google Coding Question - Making a Large Island (Hard)](https://i.ytimg.com/vi/_426VVOB8Vo/default.jpg)
![Coding Interviews Be Like](https://i.ytimg.com/vi/kVgy1GSDHG8/default.jpg)
![](https://i.ytimg.com/vi/NiMooCDxW78/default.jpg)
![Graph Coding Question - All Paths From Source To Target (LeetCode)](https://i.ytimg.com/vi/CYnvDzMprdc/default.jpg)
![Coding Interview Prep | 3 MUST KNOW Graph Problem Tips](https://i.ytimg.com/vi/tMSdjPKFRD4/default.jpg)
![GOOGLE - MAXIMUM DEPTH OF BINARY TREE (LeetCode)](https://i.ytimg.com/vi/D2cFSDfg0So/default.jpg)
![](https://i.ytimg.com/vi/UbF7LJRS5oI/default.jpg)
![Interview With A Self Taught Software Engineer Making Over 6 Figures](https://i.ytimg.com/vi/Ofw9ghQX66o/default.jpg)
![Software Engineer Salaries... How much do programmers make?](https://i.ytimg.com/vi/Btbvv9kfLqo/default.jpg)
![Longest Increasing Path in a Matrix (DFS + Memoization)](https://i.ytimg.com/vi/uLjO2LUlLN4/default.jpg)
![Is A LeetCode Premium Subscription Worth It?](https://i.ytimg.com/vi/ppySJa44sT8/default.jpg)
![Google Coding Interview Question - Longest String Chain](https://i.ytimg.com/vi/zqe_zIkyVGQ/default.jpg)
![Google Coding Interview with an ex-Microsoft Software Engineer](https://i.ytimg.com/vi/tOD6g7rF7NA/default.jpg)
![GOOGLE CODING INTERVIEW QUESTION - NUMBER OF ISLANDS (LeetCode)](https://i.ytimg.com/vi/o8S2bO3pmO4/default.jpg)
![Easy Google Coding Interview With Ben Awad](https://i.ytimg.com/vi/vHKzIPwWQkg/default.jpg)
![GOOGLE - MAX AREA OF ISLAND (LeetCode)](https://i.ytimg.com/vi/W8VuDt0eb5c/default.jpg)
![Facebook Coding Interview Question - First and Last Position in Sorted Array (LeetCode)](https://i.ytimg.com/vi/EwggFsMTxu8/default.jpg)
![Count Number of Islands using Graphs | Data Structure and Algorithms](https://i.ytimg.com/vi/ErPZFxugYkI/default.jpg)
![Number of Islands - Breadth First Search (LeetCode)](https://i.ytimg.com/vi/HS7t2i9ldmE/default.jpg)
![Google Coding Interview Question | Leetcode 1254 | Number of Closed Islands](https://i.ytimg.com/vi/tFqF6I7DiDw/default.jpg)