All problems
Island Perimeter
Problem
Given a grid of 0 water and 1 land cells, return the total perimeter around all land.
Each land cell contributes four edges, and every shared edge with the cell above or to the left removes two. Return 0 for empty or all-water grids.
Hint
Count each adjacency only once, using the top and left neighbours.
Test cases
case 1
in: [[[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]]
expected: 16
case 2
in: [[[1]]]
expected: 4
case 3
in: [[[1,0]]]
expected: 4
case 4
in: [[[1,1],[1,1]]]
expected: 8
case 5
in: [[[0]]]
expected: 0
Starter codePython
def island_perimeter(grid):
# Your code here
pass5 test cases · run your code against them in the editor