Minimum Number of Days to Disconnect Island - Problem
You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal 4-directionally (horizontal or vertical) connected group of 1's.
The grid is said to be connected if we have exactly one island, otherwise is said disconnected.
In one day, we are allowed to change any single land cell (1) into a water cell (0).
Return the minimum number of days to disconnect the grid.
Input & Output
Example 1 — Single Critical Point
$
Input:
grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
›
Output:
2
💡 Note:
The grid forms one connected island. No single cell removal disconnects it, so we need to remove 2 cells to create separation.
Example 2 — Already Disconnected
$
Input:
grid = [[1,1,0,1,1],[1,1,1,1,1],[1,1,0,1,1],[1,1,1,1,1]]
›
Output:
1
💡 Note:
Removing the center cell at (1,2) or (2,2) would disconnect the island into multiple parts.
Example 3 — Small Island
$
Input:
grid = [[1,0,1,0]]
›
Output:
0
💡 Note:
There are already 2 separate islands, so the grid is already disconnected.
Constraints
- m == grid.length
- n == grid[i].length
- 1 ≤ m, n ≤ 30
- grid[i][j] is either 0 or 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code