0% completed
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.
Example 1:
[[1, 1, 0],
[1, 1, 0],
[0, 1, 1]]
1
grid[1][1]
or grid[2][1]
) will break the connection, creating two separate islands or making it impossible to connect. Therefore, only one day is needed.Example 2:
[[1, 0, 0, 1],
[0, 1, 1, 0],
[1, 0, 0, 1]]
0
0
days to disconnect them.Example 3:
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]
2
2
days to change land to water cell at position grid[0][1]
and grid[1][0]
.Try solving this question here:
.....
.....
.....