Back to course home
0% completed
Vote For New Content
Minimum Number of Days to Disconnect Island (hard)
Problem Statement
You are given a binary grid of size m x n
, where each cell can either be land (marked as 1
) or water (marked as 0
). An island is a group of connected land cells (1
s) that are adjacent either horizontally or vertically.
The grid is considered connected if it contains exactly one island.
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.
Examples
Example 1:
- Input: grid =
[[1, 1, 0], [1, 1, 0], [0, 1, 1]]
- Expected Output:
1
- Justification: Changing any of the land cells in the first island (like
grid[1][1]
orgrid[2][1]
) will break the connection, creating two separate islands or making it impossible to connect. Therefore, only one day is needed.
Example 2:
- Input: grid =
[[1, 0, 0, 1], [0, 1, 1, 0], [1, 0, 0, 1]]
- Expected Output:
0
- Justification: There are a total 5 islands. So, we don't need
0
days to disconnect them.
Example 3:
- Input: grid =
[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]
- Expected Output:
2
- Justification: We need
2
days to change land to water cell at positiongrid[0][1]
andgrid[1][0]
.
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible