Problem
Ask
Submissions

Problem: Island Perimeter

Medium
30 min
Explore how to determine the perimeter of an island within a grid by applying matrix traversal and logical assessment of land and water cells. This lesson teaches you to analyze problem constraints and implement efficient solutions relevant to coding interview patterns.

Statement

You are given a grid with dimensions row x col, where each cell represents either land (grid[i][j] = 1) or water (grid[i][j] = 0). The grid satisfies the following conditions:

  • Cells are connected only horizontally or vertically (not diagonally).

  • The grid is surrounded by water and contains exactly one island, consisting of one or more connected land cells.

  • The island has no lakes, meaning no water is enclosed within the island that connects to the surrounding water.

  • The grid is rectangular, and each cell is a square with a side length 1.

Your task is to calculate the perimeter of the island.

Constraints:

  • row ==== grid.length

  • col ==== grid[i].length

  • 11\leq row, col 100\leq100

  • grid[i][j] is 00 or 11.

  • There is exactly one island in grid.

Problem
Ask
Submissions

Problem: Island Perimeter

Medium
30 min
Explore how to determine the perimeter of an island within a grid by applying matrix traversal and logical assessment of land and water cells. This lesson teaches you to analyze problem constraints and implement efficient solutions relevant to coding interview patterns.

Statement

You are given a grid with dimensions row x col, where each cell represents either land (grid[i][j] = 1) or water (grid[i][j] = 0). The grid satisfies the following conditions:

  • Cells are connected only horizontally or vertically (not diagonally).

  • The grid is surrounded by water and contains exactly one island, consisting of one or more connected land cells.

  • The island has no lakes, meaning no water is enclosed within the island that connects to the surrounding water.

  • The grid is rectangular, and each cell is a square with a side length 1.

Your task is to calculate the perimeter of the island.

Constraints:

  • row ==== grid.length

  • col ==== grid[i].length

  • 11\leq row, col 100\leq100

  • grid[i][j] is 00 or 11.

  • There is exactly one island in grid.