Skip to content

Commit 3ef4c85

Browse files
committed
463-island-perimeter.md Added Python solution.
1 parent 414d666 commit 3ef4c85

3 files changed

Lines changed: 177 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ You can skip the more difficult problems and do them later.
6666
# Graph Theory
6767
- [797. All Paths From Source to Target](solutions/1-1000/797-all-paths-from-source-to-target.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **2** solutions.
6868
- [200. Number of Islands](solutions/1-1000/200-number-of-islands.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_ and **3** solutions.
69+
- [463. Island Perimeter](solutions/1-1000/463-island-perimeter.md)
6970
- [695. Max Area of Island](solutions/1-1000/695-max-area-of-island.md) was solved in _Python, Java, C++, JavaScript, C#, Go, Ruby_.
7071
- [827. Making A Large Island](solutions/1-1000/827-making-a-large-island.md) was solved in _Python_.
7172
- [127. Word Ladder](solutions/1-1000/127-word-ladder.md) was solved in _Python_.

images/examples/463_1.png

1.95 KB
Loading
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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+
![](../../images/examples/463_1.png)
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+
![](../../images/graph_undirected_1.svg)
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

Comments
 (0)