FunctionRacerProblemsRace

Island Perimeter

EasyGridsPython, JavaScript, TypeScript, Java, C++

grid is made of 1 (land) and 0 (water). There is exactly one island: all land cells are linked up, left, right or down, and it has no lakes inside. Every cell is a square with side 1.

Return the length of the island's outer edge, that is, the number of cell sides that touch water or the edge of the grid.

Example 1

grid [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
returns 16

The edge of this island is 16 long.

Example 2

grid [[1]]
returns 4

A single square has 4 sides.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Grids problems