Advertisement

Number of Closed Islands - LeetCode 1254 Solution

Number of Closed Islands - Complete Solution Guide

Number of Closed Islands is LeetCode problem 1254, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given a 2D grid consists of 0s (land) and 1s (water). An island is a maximal 4-directionally connected group of 0 s and a closed island is an island totally (all left, top, right, bottom) surrounded by 1s. Return the number of closed islands . Example 1: Input: grid = [[1,1,1,1,1,1,1,0],[1,0,0,0,0,1,1,0],[1,0,1,0,1,1,1,0],[1,0,0,0,0,1,0,1],[1,1,1,1,1,1,1,0]] Output: 2 Explanation: Islands in gray are closed because they are completely surrounded by water (group of 1s). Example 2: Input: grid = [

Detailed Explanation

The problem asks us to find the number of 'closed islands' in a given 2D grid. The grid contains 0s representing land and 1s representing water. An island is a group of connected 0s (land) that are 4-directionally adjacent (up, down, left, right). A 'closed island' is an island that is completely surrounded by 1s (water). This means no part of the island touches the boundary of the grid. The input is the 2D grid, and the output is the number of closed islands.

Solution Approach

The solution uses Depth-First Search (DFS) to solve this problem. First, it identifies and eliminates all islands connected to the boundaries of the grid, since these cannot be closed islands. It does this by traversing the boundaries and using DFS to mark all connected land cells as 'visited' (changing their value to 1, effectively turning them into water). Second, it iterates through the inner portion of the grid, searching for remaining land cells (0s). Each time it finds a land cell, it increments the count of closed islands and uses DFS to mark all connected land cells of that island as visited. This ensures that each closed island is counted only once.

Step-by-Step Algorithm

  1. Step 1: Iterate through the borders of the grid (top, bottom, left, right).
  2. Step 2: For each land cell (0) on the border, perform a DFS to change all connected land cells to water (1). This eliminates islands connected to the border.
  3. Step 3: Iterate through the inner part of the grid (excluding the borders).
  4. Step 4: If a land cell (0) is found, increment the closed island count and perform a DFS to change all connected land cells to water (1).
  5. Step 5: Return the total count of closed islands.

Key Insights

  • Insight 1: Islands connected to the boundary of the grid cannot be closed islands, as they are not completely surrounded by water.
  • Insight 2: Depth-First Search (DFS) or Breadth-First Search (BFS) can be used to explore connected components (islands) in the grid.
  • Insight 3: Modifying the grid in-place can be an efficient way to track visited cells during the search, avoiding the need for extra space for a 'visited' matrix.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(m*n)

Topics

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

Companies

Asked at: DoorDash.