Skip to main content

Island Perimeter Calculator

easy
GraphDepth First SearchMatrixDfs RecursiveFloodfill
Asked atAmazonGoogleMicrosoft

Problem Description

You are given a 2D grid `landMap` of size `rows × cols` where each cell is either `1` (land) or `0` (water). There is exactly one island (a group of `1`s connected horizontally or vertically). No lakes exist inside the island.

Return the perimeter of the island.

**Example 1:**
```
Input: landMap = [[0,1,0,0],[1,1,1,0],[0,1,0,0],[1,1,0,0]]
Output: 16
```

**Example 2:**
```
Input: landMap = [[1]]
Output: 4
```

Constraints

  • 1 <= rows, cols <= 100
  • landMap[i][j] is either 0 or 1
  • The grid contains exactly one island
  • The island does not have any lakes

Follow-up

What if the grid can contain multiple islands and you must return the perimeter of the largest island?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.