Island Perimeter
Island Perimeter is LeetCode 463 (Easy). The grid holds 0 for water and 1 for land, and the land cells form one island. Each cell is a 1 × 1 square, and cells connect only up, down, left and right. Return the length of the island's outline.
- The outline is made of unit sides: a side of a land cell counts if the other side of it is water or the edge of the grid.
- The grid is at most 100 × 100, so one pass over every cell is cheap.
- 1 <= rows, cols <= 100
- grid[r][c] is 0 (water) or 1 (land)
- There is exactly one island, and it has no lakes (no water enclosed by land).
Intuition
A single land square has 4 sides. Put two land squares next to each other and the side between them disappears from the outline, and it disappears from both squares, since each had counted it.
So perimeter = 4 × (land cells) − 2 × (pairs of land cells that touch). Counting touching pairs is easy if each cell only looks up and to the left: every pair has exactly one cell that is lower or further right, so each pair is found once. Nothing about the island's shape, holes in the outline or turns needs tracing.
The answer adds up small pieces (sides of squares) and only depends on which neighbours are land. When a total depends only on neighbour pairs, a plain scan with a local check beats a graph search.
Approach
Before reading on, take a 2 × 2 block of land. Count its outline by hand, then count 4 per cell and see how many sides you must take away, and why it is 2 per touching pair.
Two ways to count the outline
Each land cell adds 4; land above or to the left takes away 2.
- Two checks per cell: only up and left.
- Shows the formula: 4 × land − 2 × pairs.
For each land cell, add 1 for every side that faces water or the grid edge.
- Literal: it counts exactly what the outline is.
- Four checks per cell: each one needs a bounds test.
Both scan the grid once. The steps, code and live demo use 4 minus shared sides; the open-sides version follows the demo.
Scan every cell, skip water
Loop over rows and columns. A water cell adds nothing to the outline, so move on. You do not need to find the island first, because every land cell gets visited by the scan anyway.
Add 4 for each land cell
Give every land cell its full 4 sides. This over-counts on purpose: a side shared with another land cell is in the total twice, once from each cell, and the next two checks remove it.
Take away 2 for land above and land to the left
If the cell above is land, subtract 2; if the cell to the left is land, subtract 2. Check r > 0 and c > 0 first, because row 0 has nothing above it and column 0 has nothing to its left; outside the grid acts like water.
Why looking only up and left is enough
Every touching pair is either one cell above the other or one beside the other. The lower or right-hand cell of the pair sees the other one when it looks up or left, and the other cell never looks down or right, so each pair loses exactly 2, once.
Code in Python, C++ and Java
Count open sides
Look at all four neighbours of each land cell. A neighbour that is outside the grid or is water means that side is part of the outline, so add 1. Each outline side belongs to exactly one land cell, so nothing is counted twice and nothing has to be subtracted.
Common pitfalls
Subtracting 1 for a shared side
if r > 0 and grid[r - 1][c] == 1:
perimeter -= 1if r > 0 and grid[r - 1][c] == 1:
perimeter -= 2The shared side was added by both cells, so it is in the total twice. For [[1, 1]] subtracting 1 gives 7, but the outline of a 1 × 2 strip is 6.
Subtracting 2 for all four neighbours
for dr, dc in ((1, 0), (-1, 0), (0, 1), (0, -1)):
if is_land(r + dr, c + dc):
perimeter -= 2for dr, dc in ((-1, 0), (0, -1)):
if is_land(r + dr, c + dc):
perimeter -= 2Both cells of a pair now find each other, so every shared side costs 4. [[1, 1]] returns 4 instead of 6. Look all four ways only if you subtract 1 each time, or count open sides instead.
Reading the row above without a bounds check in Python
if grid[r - 1][c] == 1:
perimeter -= 2if r > 0 and grid[r - 1][c] == 1:
perimeter -= 2In row 0, grid[-1] is the last row, not an error. For [[1], [1]] the top cell thinks the bottom cell is above it and subtracts 2 again, giving 4 instead of 6.
Edge cases
Sides on the border are never subtracted, because there is no cell beyond them, so they stay in the outline as they should.
Complexity
Island Perimeter FAQ
Do I need DFS or BFS to find the island first?
No. A DFS that adds 1 whenever it steps into water or off the grid also works, but it needs a visited mark and a recursion stack up to rows × cols deep. The scan touches the same cells with no extra memory, because water cells add nothing anyway.
What would change if the island had a lake, or there were several islands?
The counting code would still return the total length of every land/water border. With a lake, that includes the shore around the lake, which is why the problem rules lakes out: "perimeter" would no longer mean just the outer outline.
Is this related to Number of Islands?
Same grid, different question. Number of Islands needs a search because it asks which cells are connected; this problem only asks about neighbouring pairs, so a local check is enough.