Graph Theory37 sections · 1633 units
Open in Course

LeetCode 463 Island Perimeter - Problem Statement

LeetCode 463

You are given a 22D grid where 11 represents land and 00 represents water. The grid contains exactly one island (a connected group of land cells). The island has no lakes (water inside that is not connected to the outside water). Each cell is a square with side length 11. Find the perimeter of the island.

This problem does not need graph traversal. You can solve it by counting edges directly. For each land cell, count how many of its 44 neighbors are water or out of bounds. That count is its contribution to the perimeter.

Time: O(m×n)O(m \times n) to scan all cells. Space: O(1)O(1) since you only need counters.