LeetCode 2128: Remove All Ones With Row and Column Flips Solution

Master LeetCode problem 2128 (Remove All Ones With Row and Column Flips), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2128. Remove All Ones With Row and Column Flips

Problem Explanation

Explanation:

The problem asks us to remove all ones from a matrix by performing row and column flips. We can flip a row or a column by changing all its elements from 0 to 1 or from 1 to 0.

To solve this problem, we can iterate through each row and column of the matrix and count the number of ones. If a row or column has an odd number of ones, we can perform a flip to make it all zeros. We repeat this process until there are no more ones left in the matrix. :

Solution Code

class Solution {
    public int minFlips(int[][] mat) {
        int m = mat.length, n = mat[0].length;
        int result = Integer.MAX_VALUE;

        for (int mask = 0; mask < (1 << m); mask++) {
            int flips = 0;
            int[][] temp = new int[m][n];
            for (int i = 0; i < m; i++) {
                System.arraycopy(mat[i], 0, temp[i], 0, n);
            }

            for (int i = 0; i < m; i++) {
                if ((mask & (1 << i)) > 0) {
                    flips++;
                    for (int j = 0; j < n; j++) {
                        temp[i][j] ^= 1;
                    }
                }
            }

            for (int j = 0; j < n; j++) {
                int count = 0;
                for (int i = 0; i < m; i++) {
                    count += temp[i][j];
                }
                flips += Math.min(count, m - count);
            }

            boolean allZero = true;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    if (temp[i][j] == 1) {
                        allZero = false;
                        break;
                    }
                }
            }

            if (allZero) {
                result = Math.min(result, flips);
            }
        }

        return result == Integer.MAX_VALUE ? -1 : result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2128 (Remove All Ones With Row and Column Flips)?

This page provides optimized solutions for LeetCode problem 2128 (Remove All Ones With Row and Column Flips) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2128 (Remove All Ones With Row and Column Flips)?

The time complexity for LeetCode 2128 (Remove All Ones With Row and Column Flips) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2128 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2128 in Java, C++, or Python.

Back to LeetCode Solutions