|
| 1 | +# LeetCode 463. Island Perimeter's Solution |
| 2 | +LeetCode problem link: [463. Island Perimeter](https://leetcode.com/problems/island-perimeter) |
| 3 | + |
| 4 | +## LeetCode problem description |
| 5 | +You are given `row x col` `grid` representing a map where `grid[i][j] = 1` represents land and `grid[i][j] = 0` represents water. |
| 6 | + |
| 7 | +Grid cells are connected **horizontally/vertically** (not diagonally). The `grid` is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). |
| 8 | + |
| 9 | +The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island. |
| 10 | + |
| 11 | +### Example 1 |
| 12 | + |
| 13 | +``` |
| 14 | +Input: grid = [ |
| 15 | + [0,1,0,0], |
| 16 | + [1,1,1,0], |
| 17 | + [0,1,0,0], |
| 18 | + [1,1,0,0] |
| 19 | +] |
| 20 | +Output: 16 |
| 21 | +Explanation: The perimeter is the 16 yellow stripes in the image above. |
| 22 | +``` |
| 23 | + |
| 24 | +### Example 2 |
| 25 | +``` |
| 26 | +Input: grid = [[1]] |
| 27 | +Output: 4 |
| 28 | +``` |
| 29 | + |
| 30 | +### Example 3 |
| 31 | +``` |
| 32 | +Input: grid = [[1,0]] |
| 33 | +Output: 4 |
| 34 | +``` |
| 35 | + |
| 36 | +### Constraints |
| 37 | +- `row == grid.length` |
| 38 | +- `col == grid[i].length` |
| 39 | +- `1 <= row, col <= 100` |
| 40 | +- `grid[i][j]` is `0` or `1`. |
| 41 | +- There is exactly one island in `grid`. |
| 42 | + |
| 43 | +## Intuition |
| 44 | +The island problem can be abstracted into a **graph theory** problem. This is an **undirected graph**: |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +And this graph has only one **connected components** (island). |
| 49 | + |
| 50 | +Walk from one node to the adjacent node until all nodes on the island are visited. |
| 51 | + |
| 52 | +## Approach |
| 53 | +1. Find the first land. |
| 54 | +1. Starting at the first land, find all the lands of the island. |
| 55 | + * There are two major ways to explore a `connected components` (island): **Breadth-First Search** and **Depth-First Search**. |
| 56 | + * For **Depth-First Search**, there are two ways to make it: `Recursive` and `Iterative`. Here I will provide the `Recursive` solution. |
| 57 | + * If you want to know **Depth-First Search** `Iterative` solution, please see [200. Number of Islands (Depth-First Search by Iteration)](200-number-of-islands-2.md). |
| 58 | + * If you want to know **Breadth-First Search** solution, please see [200. Number of Islands (Breadth-First Search)](200-number-of-islands-3.md). |
| 59 | + * Mark each found land as `8` which represents `visited`. Visited lands don't need to be visited again. |
| 60 | +1. To calculate the perimeter of an island, we simply add up the number of water-adjacent edges of all water-adjacent nodes. |
| 61 | + |
| 62 | +## Complexity |
| 63 | +* Time: `O(n * m)`. |
| 64 | +* Space: `O(1)`. |
| 65 | + |
| 66 | +## Python |
| 67 | +```python |
| 68 | +class Solution: |
| 69 | + def __init__(self): |
| 70 | + self.perimeter = 0 |
| 71 | + self.grid = None |
| 72 | + |
| 73 | + def islandPerimeter(self, grid: List[List[int]]) -> int: |
| 74 | + self.grid = grid |
| 75 | + |
| 76 | + for i, row in enumerate(self.grid): |
| 77 | + for j, value in enumerate(row): |
| 78 | + if value == 1: |
| 79 | + self.depth_first_search(i, j) |
| 80 | + |
| 81 | + return self.perimeter |
| 82 | + |
| 83 | + def depth_first_search(self, i, j): |
| 84 | + if i < 0 or i >= len(self.grid): |
| 85 | + return |
| 86 | + |
| 87 | + if j < 0 or j >= len(self.grid[0]): |
| 88 | + return |
| 89 | + |
| 90 | + if self.grid[i][j] != 1: |
| 91 | + return |
| 92 | + |
| 93 | + self.grid[i][j] = 8 |
| 94 | + |
| 95 | + self.perimeter += self.water_edges(i, j) |
| 96 | + |
| 97 | + self.depth_first_search(i - 1, j) |
| 98 | + self.depth_first_search(i, j + 1) |
| 99 | + self.depth_first_search(i + 1, j) |
| 100 | + self.depth_first_search(i, j - 1) |
| 101 | + |
| 102 | + def water_edges(self, i, j): |
| 103 | + result = 0 |
| 104 | + result += self.water_edge(i - 1, j) |
| 105 | + result += self.water_edge(i, j + 1) |
| 106 | + result += self.water_edge(i + 1, j) |
| 107 | + result += self.water_edge(i, j - 1) |
| 108 | + return result |
| 109 | + |
| 110 | + def water_edge(self, i, j): |
| 111 | + if i < 0 or i >= len(self.grid): |
| 112 | + return 1 |
| 113 | + |
| 114 | + if j < 0 or j >= len(self.grid[0]): |
| 115 | + return 1 |
| 116 | + |
| 117 | + if self.grid[i][j] == 0: |
| 118 | + return 1 |
| 119 | + |
| 120 | + return 0 |
| 121 | +``` |
| 122 | + |
| 123 | +## Java |
| 124 | +```java |
| 125 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 126 | +``` |
| 127 | + |
| 128 | +## C++ |
| 129 | +```cpp |
| 130 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 131 | +``` |
| 132 | + |
| 133 | +## JavaScript |
| 134 | +```javascript |
| 135 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 136 | +``` |
| 137 | + |
| 138 | +## C# |
| 139 | +```c# |
| 140 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 141 | +``` |
| 142 | + |
| 143 | +## Go |
| 144 | +```go |
| 145 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 146 | +``` |
| 147 | + |
| 148 | +## Ruby |
| 149 | +```ruby |
| 150 | +# Welcome to create a PR to complete the code of this language, thanks! |
| 151 | +``` |
| 152 | + |
| 153 | +## C |
| 154 | +```c |
| 155 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 156 | +``` |
| 157 | + |
| 158 | +## Kotlin |
| 159 | +```kotlin |
| 160 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 161 | +``` |
| 162 | + |
| 163 | +## Swift |
| 164 | +```swift |
| 165 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 166 | +``` |
| 167 | + |
| 168 | +## Rust |
| 169 | +```rust |
| 170 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 171 | +``` |
| 172 | + |
| 173 | +## Other languages |
| 174 | +``` |
| 175 | +// Welcome to create a PR to complete the code of this language, thanks! |
| 176 | +``` |
0 commit comments