Practice/Google/Leetcode 463. Island Perimeter
CodingMust
You are given a 2D grid map where cells contain either 0 (representing water) or 1 (representing land). The grid contains exactly one island, which is a connected group of land cells joined horizontally or vertically (not diagonally).
Your task is to calculate the total perimeter of this island. The perimeter is defined as the total number of edges of land cells that border either water cells or the edge of the grid.
Example 1:
Input: grid = [[0,1,0,0], [1,1,1,0], [0,1,0,0], [1,1,0,0]] Output: 16 Explanation: The island spans multiple cells forming an irregular shape. Count each edge that faces water or the grid boundary.
Example 2:
Input: grid = [[1]] Output: 4 Explanation: A single cell island has all four sides exposed.
Example 3:
Input: grid = [[1,1], [1,1]] Output: 8 Explanation: The 2×2 square island has 8 outer edges (each of the 4 cells contributes 2 exposed sides).