Graph Theory37 sections · 1633 units
Open in Course

LeetCode 463 Island Perimeter - Core Idea

Count exposed edges

Here is the approach:

1.1. Loop through every cell in the grid.

2.2. If the cell is land (11), start with 44 edges.

3.3. Check the 44 neighbors (up, down, left, right). For each neighbor that is also land, subtract 11 from the edge count.

4.4. Add the remaining edges to the total perimeter.

5.5. Return the total after visiting all cells.

This works because each land neighbor "hides" one edge. If a cell has 33 land neighbors, only 11 edge is exposed. The simplicity comes from the fact that there is only one island, so you do not need to track visited cells.