#1284
Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
specialist · 970 · lc hard +32 · verified · 72.5% accepted · 1,007 likes · top 82%
Description
You have a m x n binary matrix mat. In a single step, pick any cell; that cell and each of its (up to four) edge-adjacent neighbors all get flipped (0 becomes 1, 1 becomes 0).
Find the minimum number of steps needed to turn mat into an all-zero matrix, or return -1 if it is impossible.
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.
Code
1
2
3