Watch 2 video solutions for Remove All Ones With Row and Column Flips II, a medium level problem involving Array, Bit Manipulation, Breadth-First Search. This walkthrough by Shuo Yan has 564 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 < m0 <= j < ngrid[i][j] == 1and change the values of all cells in row i and column j to zero.
Return the minimum number of operations needed to remove all 1's from grid.
Example 1:
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. In the second operation, change all cell values of row 0 and column 0 to zero.
Example 2:
Input: grid = [[0,1,0],[1,0,1],[0,1,0]] Output: 2 Explanation: In the first operation, change all cell values of row 1 and column 0 to zero. In the second operation, change all cell values of row 2 and column 1 to zero. Note that we cannot perform an operation using row 1 and column 1 because grid[1][1] != 1.
Example 3:
Input: grid = [[0,0],[0,0]] Output: 0 Explanation: There are no 1's to remove so return 0.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 151 <= m * n <= 15grid[i][j] is either 0 or 1.Problem Overview: You are given a binary matrix. In one operation, you pick a cell containing 1 and flip every cell in its row and column. The goal is to reach a matrix of all zeros using the minimum number of operations.
Approach 1: Backtracking Simulation (Exponential)
The most direct idea is to simulate every possible sequence of operations. Iterate through the grid, pick any cell containing 1, flip its entire row and column, and recursively continue until the grid becomes all zeros. Because multiple sequences can reach the same matrix configuration, the search quickly explodes. Without strong pruning or memoization, this approach explores an exponential number of states. Time complexity is roughly O((mn)!) in the worst case, with O(mn) space for recursion depth. This method mainly helps you understand how operations transform the grid.
Approach 2: Breadth-First Search with Matrix States (O(2^(mn) * mn))
Treat each matrix configuration as a node in a graph. From a given state, iterate through every cell; if the value is 1, generate a new state by flipping its row and column. Use Breadth-First Search to explore states level by level. A visited set prevents revisiting the same configuration. BFS guarantees the first time you reach the all-zero matrix is the minimum number of operations. The state space can reach up to 2^(mn), and generating each neighbor costs O(mn). Space complexity is also O(2^(mn)) for the visited set.
Approach 3: BFS with Bitmask Compression (O(2^(mn) * mn))
Instead of storing the matrix directly, encode it as a single integer bitmask where each bit represents a cell. This reduces memory overhead and speeds up state comparisons. When selecting a cell containing 1, compute the next mask by toggling bits for every cell in the same row and column using bit manipulation. BFS runs over these compressed states while a hash set tracks visited masks. This approach is significantly faster in practice and is the typical implementation for problems involving small matrix grids with state transitions.
Recommended for interviews: BFS with bitmask compression. It models the problem as a shortest-path search over states while keeping operations efficient. Showing the brute-force reasoning first demonstrates understanding of the state space, while the bitmask BFS demonstrates strong algorithmic optimization.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Backtracking Simulation | Exponential (≈ O((mn)!)) | O(mn) | Conceptual exploration of operation sequences; not practical for full constraints |
| BFS with Matrix States | O(2^(mn) * mn) | O(2^(mn)) | General shortest-path search across matrix configurations |
| BFS with Bitmask Compression | O(2^(mn) * mn) | O(2^(mn)) | Optimal implementation when grid size is small and state compression is possible |