LeetCode 3240: Minimum Number of Flips to Make Binary Grid Palindromic II Solution
Master LeetCode problem 3240 (Minimum Number of Flips to Make Binary Grid Palindromic II), 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.
3240. Minimum Number of Flips to Make Binary Grid Palindromic II
Problem Explanation
Explanation:
To solve this problem, we can iterate through all possible combinations of rows and columns to flip and then check if the resulting grid meets the conditions. We need to find the minimum number of flips required to make all rows and columns palindromic and the total number of 1's in the grid divisible by 4.
- We start by creating helper functions to check if a row or column is palindromic and to count the number of 1's in the grid.
- We iterate through all possible combinations of rows and columns to flip. For each combination, we flip the selected rows and columns and count the number of flips required.
- We then check if the resulting grid has all rows and columns palindromic and the total number of 1's divisible by 4.
- We keep track of the minimum number of flips required to meet the conditions.
:
Solution Code
class Solution {
public int minFlips(int[][] grid) {
int m = grid.length, n = grid[0].length;
int totalOnes = countOnes(grid);
if (totalOnes == 0 || totalOnes == m * n) return 0;
int minFlips = Integer.MAX_VALUE;
for (int rowsToFlip = 0; rowsToFlip < (1 << m); rowsToFlip++) {
for (int colsToFlip = 0; colsToFlip < (1 << n); colsToFlip++) {
int[][] flippedGrid = flipRowsAndCols(grid, rowsToFlip, colsToFlip);
if (isPalindromic(flippedGrid, m, n) && countOnes(flippedGrid) % 4 == 0) {
int flips = Integer.bitCount(rowsToFlip) + Integer.bitCount(colsToFlip);
minFlips = Math.min(minFlips, flips);
}
}
}
return minFlips == Integer.MAX_VALUE ? -1 : minFlips;
}
private int countOnes(int[][] grid) {
int count = 0;
for (int[] row : grid) {
for (int cell : row) {
count += cell;
}
}
return count;
}
private boolean isPalindromic(int[][] grid, int m, int n) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] != grid[i][n - 1 - j]) return false;
}
}
return true;
}
private int[][] flipRowsAndCols(int[][] grid, int rowsToFlip, int colsToFlip) {
int m = grid.length, n = grid[0].length;
int[][] flippedGrid = new int[m][n];
for (int i = 0; i < m; i++) {
if ((rowsToFlip & (1 << i)) != 0) {
for (int j = 0; j < n; j++) {
flippedGrid[i][j] = 1 - grid[i][j];
}
} else {
flippedGrid[i] = grid[i].clone();
}
}
for (int j = 0; j < n; j++) {
if ((colsToFlip & (1 << j)) != 0) {
for (int i = 0; i < m; i++) {
flippedGrid[i][j] = 1 - flippedGrid[i][j];
}
}
}
return flippedGrid;
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3240 (Minimum Number of Flips to Make Binary Grid Palindromic II)?
This page provides optimized solutions for LeetCode problem 3240 (Minimum Number of Flips to Make Binary Grid Palindromic II) 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 3240 (Minimum Number of Flips to Make Binary Grid Palindromic II)?
The time complexity for LeetCode 3240 (Minimum Number of Flips to Make Binary Grid Palindromic II) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3240 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3240 in Java, C++, or Python.