|
| 1 | +--- |
| 2 | +id: island-perimeter |
| 3 | +title: Island Perimeter |
| 4 | +sidebar_label: 0463-island-perimeter |
| 5 | +tags: |
| 6 | +- Array |
| 7 | +- Depth first search |
| 8 | +- Breadth first search |
| 9 | +- Matrix |
| 10 | +description: "This is a solution to the Island Perimeter problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +You are given `row x col` `grid` representing a map where `grid[i][j] = 1` represents land and `grid[i][j] = 0` represents water. |
| 16 | + |
| 17 | +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). |
| 18 | + |
| 19 | +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. |
| 20 | + |
| 21 | +### Examples |
| 22 | + |
| 23 | +**Example 1:** |
| 24 | + |
| 25 | +``` |
| 26 | +Input: grid = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]] |
| 27 | +Output: 16 |
| 28 | +Explanation: The perimeter is the 16 yellow stripes in the image above. |
| 29 | +``` |
| 30 | + |
| 31 | +**Example 2:** |
| 32 | + |
| 33 | +``` |
| 34 | +Input: grid = [[1]] |
| 35 | +Output: 4 |
| 36 | +``` |
| 37 | + |
| 38 | +### Constraints |
| 39 | + |
| 40 | +- `row == grid.length` |
| 41 | +- `col == grid[i].length` |
| 42 | +- `1 <= row, col <= 100` |
| 43 | +- `grid[i][j]` is `0` or `1`. |
| 44 | +- There is exactly one island in `grid`. |
| 45 | + |
| 46 | + |
| 47 | +### Approach |
| 48 | + |
| 49 | +#### Intuition |
| 50 | + |
| 51 | +To solve this problem, we need to calculate the total perimeter contributed by each land cell in the grid. Since we're working with a grid that has connected cells horizontally and vertically, each land cell that does not touch another land cell contributes 4 units to the perimeter (as it has four sides). |
| 52 | + |
| 53 | +Here's the intuitive step-by-step approach: |
| 54 | + |
| 55 | +1. Initialize a perimeter count to 0. |
| 56 | +2. Iterate through each cell in the grid. |
| 57 | +3. If a cell is land (1), increment the perimeter count by 4 (all possible sides of a single cell). |
| 58 | +4. Then, check the adjacent cells: |
| 59 | +- If there is a land cell to the right (horizontally adjacent), the shared edge does not contribute to the perimeter, so we subtract 2 from the perimeter count (as it removes one edge from each of the two adjacent land cells). |
| 60 | +- Similarly, if there is a land cell below (vertically adjacent), subtract 2 for the shared edge. |
| 61 | +5. Continue this process for all land cells in the grid. |
| 62 | +6. Return the total perimeter count. |
| 63 | + |
| 64 | +This approach works because it dynamically adjusts the perimeter count based on the land cell's adjacency with other land cells, ensuring that shared edges are only counted once. |
| 65 | + |
| 66 | +## Code in Different Languages |
| 67 | + |
| 68 | +<Tabs> |
| 69 | +<TabItem value="cpp" label="C++"> |
| 70 | + <SolutionAuthor name="@tanyagupta01"/> |
| 71 | + |
| 72 | +```cpp |
| 73 | +class Solution { |
| 74 | + public: |
| 75 | + int islandPerimeter(vector<vector<int>>& grid) { |
| 76 | + int islands = 0; |
| 77 | + int neighbors = 0; |
| 78 | + |
| 79 | + for (int i = 0; i < grid.size(); ++i) |
| 80 | + for (int j = 0; j < grid[0].size(); ++j) |
| 81 | + if (grid[i][j]) { |
| 82 | + ++islands; |
| 83 | + if (i - 1 >= 0 && grid[i - 1][j]) |
| 84 | + ++neighbors; |
| 85 | + if (j - 1 >= 0 && grid[i][j - 1]) |
| 86 | + ++neighbors; |
| 87 | + } |
| 88 | + |
| 89 | + return islands * 4 - neighbors * 2; |
| 90 | + } |
| 91 | +}; |
| 92 | +``` |
| 93 | +</TabItem> |
| 94 | +<TabItem value="java" label="Java"> |
| 95 | + <SolutionAuthor name="@tanyagupta01"/> |
| 96 | + |
| 97 | +```java |
| 98 | +class Solution { |
| 99 | + public int islandPerimeter(int[][] grid) { |
| 100 | + int islands = 0; |
| 101 | + int neighbors = 0; |
| 102 | + |
| 103 | + for (int i = 0; i < grid.length; ++i) |
| 104 | + for (int j = 0; j < grid[0].length; ++j) |
| 105 | + if (grid[i][j] == 1) { |
| 106 | + ++islands; |
| 107 | + if (i - 1 >= 0 && grid[i - 1][j] == 1) |
| 108 | + ++neighbors; |
| 109 | + if (j - 1 >= 0 && grid[i][j - 1] == 1) |
| 110 | + ++neighbors; |
| 111 | + } |
| 112 | + |
| 113 | + return islands * 4 - neighbors * 2; |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +</TabItem> |
| 119 | +<TabItem value="python" label="Python"> |
| 120 | + <SolutionAuthor name="@tanyagupta01"/> |
| 121 | + |
| 122 | +```python |
| 123 | +class Solution: |
| 124 | + def islandPerimeter(self, grid: List[List[int]]) -> int: |
| 125 | + m = len(grid) |
| 126 | + n = len(grid[0]) |
| 127 | + |
| 128 | + islands = 0 |
| 129 | + neighbors = 0 |
| 130 | + |
| 131 | + for i in range(m): |
| 132 | + for j in range(n): |
| 133 | + if grid[i][j] == 1: |
| 134 | + islands += 1 |
| 135 | + if i + 1 < m and grid[i + 1][j] == 1: |
| 136 | + neighbors += 1 |
| 137 | + if j + 1 < n and grid[i][j + 1] == 1: |
| 138 | + neighbors += 1 |
| 139 | + |
| 140 | + return islands * 4 - neighbors * 2 |
| 141 | +``` |
| 142 | +</TabItem> |
| 143 | +</Tabs> |
| 144 | + |
| 145 | +## Complexity Analysis |
| 146 | + |
| 147 | +### Time Complexity: O(mn) |
| 148 | + |
| 149 | +### Space Complexity: O(1) |
| 150 | + |
| 151 | +## References |
| 152 | + |
| 153 | +- **LeetCode Problem**: [Island Perimeter](https://leetcode.com/problems/island-perimeter/description/) |
| 154 | + |
| 155 | +- **Solution Link**: [Island Perimeter](https://leetcode.com/problems/island-perimeter/solutions/) |
0 commit comments