Ctrl K

Island Coastline Length

Easy
The map chart is a rectangular grid where 1 is land and 0 is water. Each cell is a square with side 1. Everything beyond the edge of the map is water. Return the total coastline on the map: the number of land-cell sides that face water (including lakes inside islands) or the edge of the map. A side shared by two land cells is not coastline.
Example 1
chart=[[1, 1, 0], [1, 1, 0], [0, 1, 0]] → 10

The 2×2 square has 8 units of shore; the cell below it adds its 4, but the side it shares with the square is not shore for either cell: 8 + 4 − 2 = 10.

Example 2
chart=[[1, 1, 1], [1, 0, 1], [1, 1, 1]] → 16

A ring around a lake: 12 sides on the outside plus 4 around the lake, 16 in total.

Example 3
chart=[[1, 0], [0, 1]] → 8

The cells touch only at a corner, so these are two separate islands with 4 sides each.

Constraints
  • 1 ≤ rows, columns ≤ 100
  • chart[i][j] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsPaint Bucket Tool EasyDrain the Landlocked Ponds MediumLand Regions on a Map MediumFrame Around an Ink Blot HardLongest Uphill Walk Hard
loading Python…
Input
chart=[[1, 1, 0], [1, 1, 0], [0, 1, 0]]
Expected
10

Run checks the first 3 tests in your browser. Submit checks all 75 tests on the server, hidden ones included.