1284. Minimum Number of Flips to Convert Binary Matrix to Zero Matrix Hard

@problem@discussion
#Array#Bit Manipulation#Breadth-First Search#Matrix



1/**
2 * [1284] Minimum Number of Flips to Convert Binary Matrix to Zero Matrix
3 *
4 * Given a m x n binary matrix mat. In one step, you can choose one cell and flip it and all the four neighbors of it if they exist (Flip is changing 1 to 0 and 0 to 1). A pair of cells are called neighbors if they share one edge.
5 * Return the minimum number of steps required to convert mat to a zero matrix or -1 if you cannot.
6 * A binary matrix is a matrix with all cells equal to 0 or 1 only.
7 * A zero matrix is a matrix with all cells equal to 0.
8 *  
9 * Example 1:
10 * <img alt="" src="https://assets.leetcode.com/uploads/2019/11/28/matrix.png" style="width: 409px; height: 86px;" />
11 * Input: mat = [[0,0],[0,1]]
12 * Output: 3
13 * Explanation: One possible solution is to flip (1, 0) then (0, 1) and finally (1, 1) as shown.
14 * 
15 * Example 2:
16 * 
17 * Input: mat = [[0]]
18 * Output: 0
19 * Explanation: Given matrix is a zero matrix. We do not need to change it.
20 * 
21 * Example 3:
22 * 
23 * Input: mat = [[1,0,0],[1,0,0]]
24 * Output: -1
25 * Explanation: Given matrix cannot be a zero matrix.
26 * 
27 *  
28 * Constraints:
29 * 
30 * 	m == mat.length
31 * 	n == mat[i].length
32 * 	1 <= m, n <= 3
33 * 	mat[i][j] is either 0 or 1.
34 * 
35 */
36pub struct Solution {}
37
38// problem: https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/
39// discuss: https://leetcode.com/problems/minimum-number-of-flips-to-convert-binary-matrix-to-zero-matrix/discuss/?currentPage=1&orderBy=most_votes&query=
40
41// submission codes start here
42
43impl Solution {
44    pub fn min_flips(mat: Vec<Vec<i32>>) -> i32 {
45        0
46    }
47}
48
49// submission codes end
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn test_1284() {
57    }
58}
59


Back
© 2025 bowen.ge All Rights Reserved.