Easy · Neighbour Edge Counting
Understand → Visualise → Practice.
Return the exposed edge count of land cells in a binary grid.
A land cell has four edges. Every edge it shares with another land cell is interior, not perimeter.
Scan the grid.
Add 4, minus one for each land neighbour above, below, left and right.
The total of exposed edges is the perimeter.
O(rows × cols) time and O(1) space.
Counting only edges on the grid border misses water inside the grid.
Read the full guide first: Matrices and 2-D Arrays.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.