Remove All Ones With Row and Column Flips II — LeetCode 2174 Python Solution

MediumLeetCode PremiumBit ManipulationBreadth-First SearchArrayMatrix
Problem
#2174
Reading time
5 min

The problem

You are given a 0-indexed m x n binary matrix grid. In one operation, you can choose any i and j that meet the following conditions: 0 <= i < m 0 <= j < n grid[i][j] == 1 and change the values of all cells in row i and column j to zero.

Example

Input
grid = [[1,1,1],[1,1,1],[0,1,0]]
Output
2
Explanation
In the first operation, change all cell values of row 1 and column 1 to zero.

Python solution

Python
class Solution:
    def removeOnes(self, grid: List[List[int]]) -> int:
        m, n = len(grid), len(grid[0])
        state = sum(1 << (i * n + j) for i in range(m) for j in range(n) if grid[i][j])
        q = deque([state])
        vis = {state}
        ans = 0
        while q:
            for _ in range(len(q)):
                state = q.popleft()
                if state == 0:
                    return ans
                for i in range(m):
                    for j in range(n):
                        if grid[i][j] == 0:
                            continue
                        nxt = state
                        for r in range(m):
                            nxt &= ~(1 << (r * n + j))
                        for c in range(n):
                            nxt &= ~(1 << (i * n + c))
                        if nxt not in vis:
                            vis.add(nxt)
                            q.append(nxt)
            ans += 1
        return -1

Complexity

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

Pattern: Bit Manipulation

Use XOR, masks and the low-bit trick to replace whole data structures with an integer. LeetCode 2174. Remove All Ones With Row and Column Flips II is filed here because LeetCode tags it Bit Manipulation, which is the vocabulary this hub collects.

The bit manipulation guide has the Python template for the pattern and the 194 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2174. Remove All Ones With Row and Column Flips II?
LeetCode 2174. Remove All Ones With Row and Column Flips II is rated Medium on LeetCode.
What is the time complexity of LeetCode 2174. Remove All Ones With Row and Column Flips II?
The Python solution on this page runs in O(V+E).
What is the space complexity of LeetCode 2174. Remove All Ones With Row and Column Flips II?
The Python solution on this page uses O(V) auxiliary space.
What topics does LeetCode 2174. Remove All Ones With Row and Column Flips II cover?
LeetCode 2174. Remove All Ones With Row and Column Flips II is tagged Bit Manipulation, Breadth-First Search, Array and Matrix on LeetCode.
Is LeetCode 2174. Remove All Ones With Row and Column Flips II a premium problem?
Yes. LeetCode 2174. Remove All Ones With Row and Column Flips II 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