543. Minimum Number of Days to Disconnect Island

HardBinary SearchGraphDFSBrute Force

Given a binary grid where 1 is land and 0 is water, a cell can be changed from land to water in one day. The grid is 'connected' if it has exactly one island (a maximal 4-directionally connected group of land). Return the minimum number of days to make the grid disconnected (zero islands or more than one island). The answer is always 0, 1, or 2. The input is JSON {grid}.

Input: JSON {grid}.

Output: Integer — the minimum days (0, 1, or 2).

Examples

Example 1
Input: {"grid":[[0,1,1,0],[0,1,1,0],[0,0,0,0]]}
Output: 2
Explanation: A solid block needs 2 removals.
Example 2
Input: {"grid":[[1,1]]}
Output: 2
Explanation: Two-cell island.
Example 3
Input: {"grid":[[1,0,1,0]]}
Output: 0
Explanation: Already disconnected (two islands).

Constraints

Asked by

Google
Solve this problem in the editor →