#1568
Minimum Number of Days to Disconnect Island
expert · 1225 · lc hard +32 · verified · 58.8% accepted · 1,299 likes · top 55%
Description
You are given an m x n binary grid grid where 1 represents land and 0 represents water. An island is a maximal group of 1s connected horizontally or vertically. Each day you may convert exactly one land cell to water. Return the minimum number of days needed so the grid no longer contains exactly one island.
Example 1:
Input: grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Example 2:
Output: 2
Explanation: 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 3:
Input: grid = [[1,1]]
Output: 2
Explanation: Grid of full water is also disconnected ([[1,1]] -> [[0,0]]), 0 islands.
Code
1
2
3