666. Remove All Ones With Row and Column Flips

You are given an m x n binary matrix grid.

In one operation, you can choose any row or column and flip each value in that row or column (i.e., changing all 0's to 1's, and all 1's to 0's).

Return true if it is possible to remove all 1's from grid using any number of operations or false otherwise.

Example 1:

Input: grid = [[0,1,0],[1,0,1],[0,1,0]]

Output: true

Explanation: One possible way to remove all 1's from grid is to:

- Flip the middle row

- Flip the middle column

Example 2:

Input: grid = [[1,1,0],[0,0,0],[0,0,0]]

Output: false

Explanation: It is impossible to remove all 1's from grid.

Now Your Turn!

Pick the correct output for the given input

Consider the grid = [[1,0,1], [0,1,0], [1,0,1]]. What is the minimum number of flips required to remove all 1s?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • grid[i][j] is either 0 or 1.

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
bool removeOnes(vector<vector<int>>& grid) {
// Your code goes here
}
};
Test Case

Input:

Grid