Measure the Boundary of One Island

Quick Overview

Measure the unit-length land-water boundary of a single orthogonally connected island in a rectangular binary grid, including exposed sides at the grid boundary.

Measure the Boundary of One Island

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

# Measure the Boundary of One Island A rectangular binary grid describes a map: `1` is land and `0` is water. Land cells connect through horizontal or vertical sides, and there is exactly one island containing at least one land cell. Each cell is a unit square. The area beyond the grid is water, and there are no enclosed lakes inside the island. Return the total length of the island's land-water boundary. Land may touch a grid edge; those exposed sides also belong to the perimeter. ### Interface `class Solution { public: int islandPerimeter(vector<vector<int>>& grid); };` ### Example 1 ```text Input: grid = [[1,1,1],[0,1,0]] Output: 10 ``` The four land squares form a T shape. Their outside boundary has ten unit-length sides. ### Example 2 ```text Input: grid = [[1,1],[1,1]] Output: 8 ``` The island is a 2-by-2 square whose boundary has length 8. ### Constraints - `row == grid.length` and `col == grid[i].length`. - `1 <= row, col <= 100`. - Each `grid[i][j]` is 0 or 1. - There is exactly one horizontally/vertically connected island. - The island has no lakes. ### What a Strong Answer Covers - Unit boundary lengths, shared land sides and grid-edge exposure. - Thin island shapes and solid blocks without treating diagonal contact as an edge. - Time and auxiliary space for the full 100-by-100 grid. ```hint Shared sides How does joining two land squares along one full side affect which of their sides remain on the boundary? ```

Overview: Measure the unit-length land-water boundary of a single orthogonally connected island in a rectangular binary grid, including exposed sides at the grid boundary.

Google logo
Google
easySoftware EngineerOtherCoding & Algorithms
0
0

Measure the Boundary of One Island

A rectangular binary grid describes a map: 1 is land and 0 is water. Land cells connect through horizontal or vertical sides, and there is exactly one island containing at least one land cell.

Each cell is a unit square. The area beyond the grid is water, and there are no enclosed lakes inside the island. Return the total length of the island's land-water boundary. Land may touch a grid edge; those exposed sides also belong to the perimeter.

Interface

class Solution { public: int islandPerimeter(vector<vector<int>>& grid); };

Example 1

Input: grid = [[1,1,1],[0,1,0]]
Output: 10

The four land squares form a T shape. Their outside boundary has ten unit-length sides.

Example 2

Input: grid = [[1,1],[1,1]]
Output: 8

The island is a 2-by-2 square whose boundary has length 8.

Constraints

  • row == grid.length and col == grid[i].length .
  • 1 <= row, col <= 100 .
  • Each grid[i][j] is 0 or 1.
  • There is exactly one horizontally/vertically connected island.
  • The island has no lakes.

What a Strong Answer Covers Guidance

  • Unit boundary lengths, shared land sides and grid-edge exposure.
  • Thin island shapes and solid blocks without treating diagonal contact as an edge.
  • Time and auxiliary space for the full 100-by-100 grid.