LeetCode Problem

How to Solve Minimum Number of Days to Disconnect Island

To solve the Minimum Number of Days to Disconnect Island problem, you need to identify how to separate an island in a grid in the fewest possible days. The approach involves using depth-first search (DFS) to identify the connected components and the minimum number of land cells that need to be changed to water to break the connectivity.

GhostInterview Help

Need help with Minimum Number of Days to Disconnect Island without spending extra time grinding it?

GhostInterview can read Minimum Number of Days to Disconnect Island from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1568Array plus Depth-First SearchReviewed 2026-03-07
Difficulty
Hard
Primary pattern
Array plus Depth-First Search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

To solve the Minimum Number of Days to Disconnect Island problem, you need to identify how to separate an island in a grid in the fewest possible days. The approach involves using depth-first search (DFS) to identify the connected components and the minimum number of land cells that need to be changed to water to break the connectivity.

Problem Statement

You are given an m x n binary grid where 1 represents land and 0 represents water. An island is defined as a group of 1's connected horizontally or vertically. The grid is considered connected if there is exactly one island; otherwise, it is disconnected.

In one day, you are allowed to change any single land cell (1) to a water cell (0). Your task is to determine the minimum number of days required to disconnect the island. If the grid is already disconnected, return 0.

Examples

Example 1

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

Output: 2

We need at least 2 days to get a disconnected grid. Change land grid[1][1] and grid[0][2] to water and get 2 disconnected island.

Example 2

Input: grid = [[1,1]]

Output: 2

Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 30
  • grid[i][j] is either 0 or 1.

Solution Approach

Check if the Grid is Already Disconnected

Before attempting any modifications, first check if the grid is already disconnected. If the grid has no islands or multiple disconnected islands, then 0 days are required. This step is essential to avoid unnecessary changes and optimize the process.

Apply Depth-First Search (DFS) to Find Island

Use depth-first search (DFS) to explore the grid and locate the existing island. DFS will help you identify the connected components of land and mark cells that need to be altered in order to break the connectivity.

Modify the Grid to Disconnect the Island

To disconnect the island, determine the minimum number of land cells (1's) that need to be changed to water (0's) in order to separate the island. Changing any land cell strategically to water will isolate the components of the island. Track the number of changes required to achieve disconnection.

Complexity Analysis

MetricValue
TimeO(m \cdot n)
SpaceO(m \cdot n)

The time complexity of this solution is O(m * n) since we perform a DFS search through the grid, which involves visiting each cell at most once. The space complexity is O(m * n) due to the recursion stack used by DFS.

What Interviewers Usually Probe

  • Check if the candidate is able to identify the condition when the grid is already disconnected.
  • Look for the candidate's ability to explain depth-first search and its relevance to finding connected components in a grid.
  • Assess if the candidate considers the grid size and optimizes the approach accordingly.

Common Pitfalls or Variants

Common pitfalls

  • Overlooking the case when the grid is already disconnected and returning unnecessary steps.
  • Not properly handling edge cases such as small grid sizes or grids that are already water.
  • Failing to optimize the DFS or grid traversal to minimize the number of operations required.

Follow-up variants

  • Allowing modifications to diagonal connections in the grid instead of only vertical and horizontal connections.
  • Implementing the solution using a breadth-first search (BFS) instead of DFS.
  • Handling multiple islands and calculating the number of days to disconnect all of them.

How GhostInterview Helps

  • GhostInterview provides efficient strategies for optimizing grid traversal, ensuring your DFS solution is effective and scalable.
  • With GhostInterview's approach, you will learn how to identify when a grid is already disconnected, saving time and computation.
  • GhostInterview guides you through breaking down problems into smaller steps, such as isolating connected components and minimizing changes in the grid.

Topic Pages

FAQ

What is the primary algorithm used to solve the Minimum Number of Days to Disconnect Island problem?

The primary algorithm used is Depth-First Search (DFS) to identify connected components in the grid and then determine the minimum number of changes required to disconnect the island.

How do I handle the case where the grid is already disconnected?

If the grid is already disconnected, simply return 0, as no changes are necessary.

Can the problem be solved using Breadth-First Search (BFS) instead of DFS?

Yes, BFS can also be used for exploring the grid, but DFS is often preferred for this type of problem because it is simpler for handling recursion and grid traversal.

What are the time and space complexity of this problem?

The time complexity is O(m * n), and the space complexity is O(m * n), where m is the number of rows and n is the number of columns in the grid.

What is the minimum number of changes required to disconnect an island in a grid?

The minimum number of changes is the smallest number of land cells that must be changed to water to isolate the island, which can be determined using DFS.

GhostInterview Solver

Need direct help with Minimum Number of Days to Disconnect Island instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Number of Days to Disconnect Island from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.