File: island-perimeter/solution.py

Date: 2026-06-06

Time: 17:08

Purpose

This file solves LeetCode 463 — Island Perimeter. Given a 2D grid where 1 represents land and 0 represents water, it computes the perimeter of the single island. The problem guarantees exactly one island with no lakes (no water enclosed by land).

Key Components

Solution.islandPerimeter(grid: List[List[int]]) -> int — The sole method. Scans every cell in the grid and accumulates the perimeter using an additive-then-subtract approach.

Patterns

Neighbor-subtraction counting. Rather than checking all four neighbors to count exposed edges, this solution uses a well-known optimization:

1. Each land cell starts contributing 4 edges.

2. For each shared edge with an already-visited land neighbor, subtract 2 (one edge from each cell is internal).

Only the up and left neighbors are checked (lines 17–20), not all four directions. This works because the scan goes top-to-bottom, left-to-right — so by the time we visit cell (r, c), cells (r-1, c) and (r, c-1) have already been processed. Each adjacency is counted exactly once, and each adjacency eliminates 2 perimeter edges (one from each of the two adjacent cells).

Dependencies

Imports: typing.List — used only for the type annotation on grid.

Imported by: island-perimeter/test_solution.py consumes this directly. The massive "Imported By" list in the prompt is an artifact of all test files importing List from typing, not importing this module.

Flow


for each cell (r, c) in row-major order:
    if land:
        perimeter += 4
        if cell above is land:  perimeter -= 2
        if cell to left is land: perimeter -= 2
return perimeter

No recursion, no BFS/DFS, no visited set. A single O(rows × cols) pass.

Invariants

Error Handling

None. The method trusts its input matches the LeetCode contract — a rectangular grid of 0s and 1s with exactly one island. No validation, no exceptions.

Topics to Explore

Beliefs