675. Minimum Operations to Remove Adjacent Ones in Matrix

Given a 0-indexed binary matrix grid, where each cell contains either 0 or 1. In one operation, you can flip any 1 to 0.

A binary matrix is well-isolated if no 1 is 4-directionally (up, down, left, or right) adjacent to another 1.

Return the minimum number of operations required to make grid well-isolated.

Example 1:

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

Output: 3

Explanation:

Flipping grid[0][1], grid[1][2], and grid[2][1] to 0 makes the matrix well-isolated.

Example 2:

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

Output: 0

Explanation:

Since there are no 1s, the grid is already well-isolated. No operations are needed.

Now Your Turn!

Pick the correct output for the given input

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

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:
int minimumOperations(vector<vector<int>>& grid) {
}
};
 
Test Case

Input:

Grid