Minimum Number of Days to Disconnect Island — LeetCode 1568 Python Solution

HardDepth-First SearchBreadth-First SearchArrayMatrixStrongly Connected Component
Problem
#1568
Reading time
6 min

The 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.

Example

Input
grid = [[0,1,1,0],[0,1,1,0],[0,0,0,0]]
Output
2
Explanation
We need at least 2 days to get a disconnected grid.

Python solution

Python
class Solution:
    def minDays(self, grid: List[List[int]]) -> int:
        if self.count(grid) != 1:
            return 0
        m, n = len(grid), len(grid[0])
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    grid[i][j] = 0
                    if self.count(grid) != 1:
                        return 1
                    grid[i][j] = 1
        return 2

    def count(self, grid):
        def dfs(i, j):
            grid[i][j] = 2
            for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]:
                x, y = i + a, j + b
                if 0 <= x < m and 0 <= y < n and grid[x][y] == 1:
                    dfs(x, y)

        m, n = len(grid), len(grid[0])
        cnt = 0
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 1:
                    dfs(i, j)
                    cnt += 1
        for i in range(m):
            for j in range(n):
                if grid[i][j] == 2:
                    grid[i][j] = 1
        return cnt

Complexity

MeasureComplexity
TimeO(V+E)
SpaceO(V) auxiliary

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 1568. Minimum Number of Days to Disconnect Island is filed here because LeetCode tags it Matrix, which is the vocabulary this hub collects.

The matrix and grid guide has the Python template for the pattern and the 216 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 1568. Minimum Number of Days to Disconnect Island?
LeetCode 1568. Minimum Number of Days to Disconnect Island is rated Hard on LeetCode.
What is the time complexity of LeetCode 1568. Minimum Number of Days to Disconnect Island?
The Python solution on this page runs in O(V+E).
What is the space complexity of LeetCode 1568. Minimum Number of Days to Disconnect Island?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 1568. Minimum Number of Days to Disconnect Island cover?
LeetCode 1568. Minimum Number of Days to Disconnect Island is tagged Depth-First Search, Breadth-First Search, Array, Matrix and Strongly Connected Component on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview