class Solution:
def islandPerimeter(self, grid: List[List[int]]) -> int:
rows = len(grid)
cols = len(grid[0])
perimeter = 0
for r in range(rows):
for c in range(cols):
if grid[r][c] == 1:
# Each land cell has 4 sides, subtract sides shared with other land
perimeter += 4
if r > 0 and grid[r-1][c] == 1:
perimeter -= 2 # shared with top neighbor
if c > 0 and grid[r][c-1] == 1:
perimeter -= 2 # shared with left neighbor
return perimeter
class Solution {
public:
int islandPerimeter(vector<vector<int>>& grid) {
int rows = grid.size();
int cols = grid[0].size();
int perimeter = 0;
for (int r = 0; r < rows; ++r) {
for (int c = 0; c < cols; ++c) {
if (grid[r][c] == 1) {
perimeter += 4;
if (r > 0 && grid[r-1][c] == 1)
perimeter -= 2;
if (c > 0 && grid[r][c-1] == 1)
perimeter -= 2;
}
}
}
return perimeter;
}
};
class Solution {
public int islandPerimeter(int[][] grid) {
int rows = grid.length;
int cols = grid[0].length;
int perimeter = 0;
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) {
if (grid[r][c] == 1) {
perimeter += 4;
if (r > 0 && grid[r-1][c] == 1)
perimeter -= 2;
if (c > 0 && grid[r][c-1] == 1)
perimeter -= 2;
}
}
}
return perimeter;
}
}
var islandPerimeter = function(grid) {
let rows = grid.length;
let cols = grid[0].length;
let perimeter = 0;
for (let r = 0; r < rows; r++) {
for (let c = 0; c < cols; c++) {
if (grid[r][c] === 1) {
perimeter += 4;
if (r > 0 && grid[r-1][c] === 1)
perimeter -= 2;
if (c > 0 && grid[r][c-1] === 1)
perimeter -= 2;
}
}
}
return perimeter;
};
You are given a 2D grid map where each cell is either land (1
) or water (0
). The grid represents a map containing exactly one island (a group of connected lands), with no lakes (water inside that isn't connected to the water around the island). The island is surrounded by water, and there are no diagonally connected lands (only horizontal or vertical connections are valid).
Your task is to compute the perimeter of the island. The perimeter is defined as the total length of the island's boundary—the number of edges that are adjacent to water or the grid boundary.
Constraints:
At first glance, it might seem like we need to traverse the entire grid and check every possible edge of every land cell. The brute-force way would be to look at every land cell and, for each of its four sides, check if that side is either at the edge of the grid or adjacent to water. If so, we count it as part of the perimeter.
However, we can optimize this process. Instead of checking all four neighbors for every land cell, we can notice that each land cell contributes 4 edges to the perimeter. But whenever two land cells are adjacent (either vertically or horizontally), they share an edge, which means we count that edge twice if we just sum 4 for every cell. So, for every shared edge, we need to subtract 2 from the total perimeter (since both cells would have counted it once).
Therefore, by only checking the top and left neighbors for each land cell (to avoid double counting), we can efficiently compute the perimeter in a single pass through the grid.
Let's break down the steps to solve the problem efficiently:
1
):
This approach ensures we only count each shared edge once, and we don't double-count any boundaries.
Let's use the following grid as an example:
grid = [ [0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0] ]
Let's walk through the algorithm:
perimeter = 0
1
):
The final perimeter is 16.
The key to solving the Island Perimeter problem efficiently is to recognize that each land cell contributes 4 to the perimeter, but each shared edge between two lands should only be counted once (by subtracting 2 for each shared edge). By only checking the top and left neighbors for each land cell, we avoid double-counting and can compute the perimeter in a single pass. This approach is simple, efficient, and easy to understand, making it an elegant solution to the problem.