Advertisement

Island Perimeter - LeetCode 463 Solution

Island Perimeter - Complete Solution Guide

Island Perimeter is LeetCode problem 463, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

You are given row x col grid representing a map where grid[i][j] = 1 represents land and grid[i][j] = 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes", meaning the water inside isn't connected to the water around the island. One cell is a square with side length 1. The grid is rectangular, width and height don'

Detailed Explanation

The problem asks us to calculate the perimeter of an island represented by a 2D grid. The grid contains 1s representing land and 0s representing water. The island is surrounded by water, has no lakes (internal water not connected to the outside), and is made up of connected land cells horizontally or vertically. Each cell is a square of side length 1, and the grid dimensions are at most 100x100. The task is to find the total length of the perimeter of the island.

Solution Approach

The solution iterates through the grid, cell by cell. When a land cell (grid[i][j] == 1) is encountered, it initially adds 4 to the perimeter. Then, it checks the adjacent cells (up and left) to see if they are also land. If an adjacent cell is land, it means they share an edge, so 2 is subtracted from the perimeter. This process continues until all cells in the grid have been checked.

Step-by-Step Algorithm

  1. Step 1: Initialize the perimeter to 0.
  2. Step 2: Iterate through each cell in the grid using nested loops.
  3. Step 3: For each cell, check if it's land (grid[i][j] == 1).
  4. Step 4: If the cell is land, add 4 to the perimeter.
  5. Step 5: Check if the cell above (i-1, j) is within bounds and is land. If so, subtract 2 from the perimeter.
  6. Step 6: Check if the cell to the left (i, j-1) is within bounds and is land. If so, subtract 2 from the perimeter.
  7. Step 7: After iterating through all cells, return the calculated perimeter.

Key Insights

  • Insight 1: Each land cell contributes 4 to the total perimeter if it's completely isolated. However, neighboring land cells reduce the perimeter by 2 for each shared edge.
  • Insight 2: We can iterate through the grid and, for each land cell, check its adjacent cells to see if they are also land. If they are, we subtract 2 from the total perimeter because a shared edge exists.
  • Insight 3: Handle boundary checks carefully to avoid accessing elements outside the grid. Consider only horizontal and vertical neighbors, not diagonal.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(1)

Topics

This problem involves: Array, Depth-First Search, Breadth-First Search, Matrix.

Companies

Asked at: Cadence.