Minimum Operations to Remove Adjacent Ones in Matrix — LeetCode 2123 Python Solution

HardLeetCode PremiumGraphArrayMatrix
Problem
#2123
Reading time
6 min

The problem

You are given a 0-indexed binary matrix grid. In one operation, you can flip any 1 in grid to be 0.

Example

Input
grid = [[1,1,0],[0,1,1],[1,1,1]]
Output
3
Explanation
Use 3 operations to change grid[0][1], grid[1][2], and grid[2][1] to 0.

Python solution

Python
class Solution:
    def minimumOperations(self, grid: List[List[int]]) -> int:
        def find(i: int) -> int:
            for j in g[i]:
                if j not in vis:
                    vis.add(j)
                    if match[j] == -1 or find(match[j]):
                        match[j] = i
                        return 1
            return 0

        g = defaultdict(list)
        m, n = len(grid), len(grid[0])
        for i, row in enumerate(grid):
            for j, v in enumerate(row):
                if (i + j) % 2 and v:
                    x = i * n + j
                    if i < m - 1 and grid[i + 1][j]:
                        g[x].append(x + n)
                    if i and grid[i - 1][j]:
                        g[x].append(x - n)
                    if j < n - 1 and grid[i][j + 1]:
                        g[x].append(x + 1)
                    if j and grid[i][j - 1]:
                        g[x].append(x - 1)

        match = [-1] * (m * n)
        ans = 0
        for i in g.keys():
            vis = set()
            ans += find(i)
        return ans

Complexity

MeasureComplexity
TimeO(m \times n), where n and m are the number of 1s in the matrix and the number of edges, respectively
SpaceO(V) auxiliary

Pattern: Matrix and Grid

Treat a 2-D grid as a graph whose neighbours are the four adjacent cells. LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix 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 2123. Minimum Operations to Remove Adjacent Ones in Matrix?
LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix is rated Hard on LeetCode.
What is the time complexity of LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix?
The Python solution on this page runs in O(m \times n), where n and m are the number of 1s in the matrix and the number of edges, respectively.
What is the space complexity of LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix cover?
LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix is tagged Graph, Array and Matrix on LeetCode.
Is LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix a premium problem?
Yes. LeetCode 2123. Minimum Operations to Remove Adjacent Ones in Matrix is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

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