Class Solution
- java.lang.Object
-
- g1201_1300.s1284_minimum_number_of_flips_to_convert_binary_matrix_to_zero_matrix.Solution
-
public class Solution extends Object
1284 - Minimum Number of Flips to Convert Binary Matrix to Zero Matrix.Hard
Given a
m x nbinary matrixmat. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing1to0and0to1). A pair of cells are called neighbors if they share one edge.Return the minimum number of steps required to convert
matto a zero matrix or-1if you cannot.A binary matrix is a matrix with all cells equal to
0or1only.A zero matrix is a matrix with all cells equal to
0.Example 1:

Input: mat = [[0,0],[0,1]]
Output: 3
Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
Example 2:
Input: mat = [[0]]
Output: 0
Explanation: Given matrix is a zero matrix. We do not need to change it.
Example 3:
Input: mat = [[1,0,0],[1,0,0]]
Output: -1
Explanation: Given matrix cannot be a zero matrix.
Constraints:
m == mat.lengthn == mat[i].length1 <= m, n <= 3mat[i][j]is either0or1.
-
-
Constructor Summary
Constructors Constructor Description Solution()
-