grid
0
1
0
0
1
1
1
0
0
1
0
0
Brute force · check four sides
▸1perimeter ← 02for each land cell: perimeter ← perimeter + (sides facing water or the border)3return perimeter
state
- perimeter0
warming up the animation
A grid of 0s (water) and 1s (land) contains exactly one island with no lakes. Return the perimeter of that island.
▸1perimeter ← 02for each land cell: perimeter ← perimeter + (sides facing water or the border)3return perimeter
line 1For every land cell, look at all four sides and count the ones facing water or the edge of the grid.